I have an ASMX web service that is successfully responding with the following JSON:
[{
"Id": 49,
"SupplierName": "Supplier 1"
}, {
"Id": 50,
"SupplierName": "Supplier 2"
}, {
"Id": 51,
"SupplierName": "Supplier 3"
}, {
"Id": 52,
"SupplierName": "Supplier 4"
}]
I am trying to get my ajax call to loop through this array and append the results to a select control in my form. Unfortunately, I am fairly new to using ajax and have not been able to accomplish this yet. Any guidance would be helpful.
$.ajax({
type: "POST",
url: "/StockPileDelivery.asmx/PopulateSupplierDdl",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var data = $.parseJSON(msg.d);
$.each(data, function (i, item) {
alert(i);
// $("#dropDownCars").append("<option>" + value.carsName + "</option>");
});
},
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
},
});
UPDATE Below is my revised AJAX which is returning an error stating "Cannot use 'in' operator to search for '158' in
$.ajax({
type: "POST",
url: "/StockPileDelivery.asmx/PopulateSupplierDdl",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var data = msg.d;
$.each(data, function (i, item) {
alert(item.Id);
//$("#Suppliers").append("<option value='" + item.Id + "'>" + item.SupplierName + "</option>");
});
},
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
},
});
$.parseJSON()whendataType:'json'is set. The data passed tosuccesswill already be a javascript object or array