Naren Naren - 1 month ago 13
jQuery Question

Pass array list to $.ajax post in jQuery

My web API accepts below JSON format (this is input parameter)

{
"districtID": "string",
"legendIDs": [
"string","string"
]
}


So I have build the sample JSON and calling the $.ajax request below in my javascript button click event

var cityDistrictId = '14a3dd98-34e2-451f-8513-7a7c9d61fcc4'

var legendIds = '"[00D18EECC4A1B4DBC000011202,00D18EECC4A1CCB9B400011202]"'

var atrDistrictLegendIds = { districtID: cityDistrictId, legendIDs: legendIds };

var test = JSON.stringify(atrDistrictLegendIds);

$.support.cors = true;

var jqxhr =$.ajax({
url: "http://localhost:5000/api/SpaClassLegend/DistrictLegend",
type: "PUT",
dataType: Boolean,
global: false,
async: false,
contentType: "application/json; charset=utf-8",
data: test,
success: function (resultData) {
result = resultData;
}
}).responseText;

alert(jqxhr);


I am getting below error in alert message

{"legendIDs":["The input was not valid."]}


I am not sure, am I sending the right way or not.
Kindly please help me on this.

Answer

legendIDs accepts an array of strings. So you should be specifying

var legendIds = ['00D18EECC4A1B4DBC000011202', '00D18EECC4A1CCB9B400011202']