Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My json response is having the correct data as shown below. I need to iterate through the items array in Data response.


{"Data":{"OrderNo":67,"CustomerId":2,"OrderDate":"/Date(1574361000000)/","OrderAmount":100,"CustomerMaster":null,"Items":[{"ProductId":3,"Qty":7,"Price":9,"Total":63},{"ProductId":2,"Qty":8,"Price":9,"Total":72}]}}



var OrderNo = $("#OrderNo").val();


        $.ajax({
            type: "POST",
            url: "/Orders/ShowOrder",
            data: { "id": OrderNo },
            success: function (Data) {
                var res = Data;
                alert(JSON.stringify(Data));
        
                               

                var tblorder = $("#tblOrderitems");
                $.each(Data, function (index, item) {
                    var tr = $("<tr></tr>");
                    tr.html(("<td>" + item.ProductId + "</td>")
                    + " " + ("<td>" + item.Qty + "</td>")
                    + " " + ("<td>" + item.Price + "</td>")
                    + " " + ("<td>" + item.Total + "</td>")
                    );
                    tblorder.append(tr);
                })

            }
        });


What I have tried:

I have tried Data.Items[0].productId but shows error
Posted
Updated 22-Nov-19 21:48pm

1 solution

Small modification in script is sufficient enough.

That is, change "Data" to "res.Data.Items".

Below is the updated line for the same

JavaScript
$.each(res.Data.Items, function (index, item) {


Please note javascript is case-sensitive. You should try "ProdutId" where 'P' and 'I' are capitals.
 
Share this answer
 
Comments
Mahi8089 23-Nov-19 3:49am    
Yes, I found it...using Data.Data.Items.

thanks
GSThakur 23-Nov-19 12:32pm    
Feel free to mark this as answer if this worked for you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900