0

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);
            },
        });
1
  • 1
    no need to use $.parseJSON() when dataType:'json' is set. The data passed to success will already be a javascript object or array Commented Jul 13, 2017 at 14:32

2 Answers 2

2

The data already is an object here, that's why I didn't do the parseJSON method. Here is a working fiddle:

var msg = [{
    "Id": 49,
    "SupplierName": "Supplier 1"
}, {
    "Id": 50,
    "SupplierName": "Supplier 2"
}, {
    "Id": 51,
    "SupplierName": "Supplier 3"
}, {
    "Id": 52,
    "SupplierName": "Supplier 4"
}];

$(function(){
	$.each(msg, function (i, item) {
    	$("#dropDownCars").append("<option value='"+item.Id+"'>" + item.SupplierName + "</option>");
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropDownCars">
</select>

Sign up to request clarification or add additional context in comments.

6 Comments

I see your code works, but for some reason it is not working in my ajax success call. See updated code above.
Can you please add a "console.log(data);" after "var data = msg.d;" and tell me the output?
[{"Id":49,"SupplierName":"Supplier 1"},{"Id":50,"SupplierName":"Supplier 2"},{"Id":51,"SupplierName":"Supplier 3"},{"Id":52,"SupplierName":"Supplier 4"}]
Parsing seems to fix the issue. I wonder if this is because I am calling an ASMX web service. I have it responding with JSON, but that is the only difference between your code and mine.
That is what I thought too. Now my OCD is kicking in and I have to spend time to see why it doesn't work without parsing. Thank you for your help.
|
0

On the face of it your ajax looks ok

Have a look at the $.each documentation - http://api.jquery.com/jquery.each/

i in your function is the index, item is the actual value. alert(item) (or better yet console.log(item)) would confirm this.

You probably then want something like

$("#dropDownCars").append("<option>" + item.SupplierName+ "</option>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.