My Web API cannot recognize array parameters.
I have an array in front-end like this [1, 2, 3, 4, 5]. Now I want to pass it from the JSON body(not URL) to a Web API as follow:
Back-end Code:
// POST /api/result
[HttpPost]
public IHttpActionResult GetResult([FromBody]int[] ids)
{
//do something
}
Front-end code(jQuery):
var array = new Array("1", "2", "3", "4", "5");
var ids = Json.stringify(array);
$.ajax({
type: "POST",
url: "/api/result",
data: ids,
success: function (data, state) {
alert("success!");
},
dataType: "json"
})
But what the api got is "[]" instead of ["1", "2", "3", "4", "5"].
How to solve this problem?
data: {ids:ids},try passing with JSON format in your ajax