2
$.getJSON("suppliermanagement.ashx", { action: "gsupplierinvoice", SupllierID: Supplierid }, function (data) {
    alert("purchaseinvoice called");
    alert(JSON.stringify(data))
    var JsonInvoice = JSON.stringify(data);
    $.each(data, function (key, item) {
        // var invdata = data[i];

        $("#Gridinvoice").append('<tr><td class="auto-style36" style="border-style: solid; border-width: thin">' + item.InvoiceDate + '</td><td class="auto-style36" style="border-style: solid; border-width: thin">' + item.SuppliersInvoiceNumber + '</td><td class="auto-style36" style="border-style: solid; border-width: thin"></td><td class="auto-style36" style="border-style: solid; border-width: thin">' + item.Type + '<td></tr>')

    });           
});

Above is my function to get the data in json format but I am not able to append using for loop and foreach loop. It shows undefined. How to append data in the table using for loop?

Here Is the result of JSON.stringify(data)

{"Dpurchaseinvoice":[{"pInvoiceDate":"/Date(1405708200000)/","pSupplierInvoiceNumber":"G003","pType":1},{"pInvoiceDate":"/Date(1405708200000)/","pSupplierInvoiceNumber":"H008","pType":1}],"ErrorMessage":"","mID":0,"mJobID":null,"mSupplierID":null,"mInvoiceDate":"/Date(-62135596800000)/","mOurRef":0,"mSupplierInvoiceNumber":null,"mPurchaseOrderRef":null,"mType":0,"mPaid":null,"mReferencePurchaseInvoiceID":null,"supplier":null,"JobID":null,"ID":0,"SupplierID":null,"InvoiceDate":"/Date(-62135596800000)/","OurRef":0,"SuppliersInvoiceNumber":null,"PurchaseOrderRef":null,"Type":0,"Paid":null,"ReferencePurchaseInvoiceID":null,"ErrorSummary":null,"ErrorList":[]}

4
  • 1
    Welcome to Stack Overflow. You can format source code with the Code Sample {} toolbar button—I've done it for you this time. Commented Jul 17, 2014 at 9:22
  • 2
    could you also post the sample data that was returned from "suppliermanagement.ashx"? Commented Jul 17, 2014 at 9:25
  • maybe returned data is not a json format Commented Jul 17, 2014 at 9:30
  • returned data must be in a form of an Object {} or an Array[]. If the data you entered has further information inside, you have to retrieve that first. Let us say you are returning an Object with an arrayField which contains the data you actually want, you need to enter data.arrayField into the .each loop instead. Commented Jul 17, 2014 at 9:35

1 Answer 1

1

Your code works. Perhaps the selector #Gridinvoice is wrong.

EDIT: Here is your code, updated to use the JSON data:

$.each(data.Dpurchaseinvoice, function (key, item) {
    $("#Gridinvoice").append('<tr><td class="auto-style36" style="border-style: solid; border-width: thin">' + item.pInvoiceDate + '</td><td class="auto-style36" style="border-style: solid; border-width: thin">' + item.pSupplierInvoiceNumber + '</td><td class="auto-style36" style="border-style: solid; border-width: thin"></td><td class="auto-style36" style="border-style: solid; border-width: thin">' + item.pType + '<td></tr>')
});

Notice the data.Dpurchaseinvoice and the prefix item.pXXX.

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

6 Comments

"#Gridinvoice" is tble ID.I am appending the Row on that table.i got the row appended but i cant get the perfect data in the row.Data is undefined in the "td".And if i will not use the for loop or $.each the i can get only one row from the database.
Use "console.log" instead of "alert". Open the console (CTRL + SHIFT + I in Chrome or Firefox), copy the printed content for "JSON.stringify(data)" here.
@user2835692, please post the data that was retrieved from the server, otherwise we can only assume..
@user2835692, Please paste the result of "console.log(JSON.stringify(data));". Don't edit my post (but you can edit yours!) and don't forget the "JSON.stringify".
@Tarh I have attached Json.Stringify to my post.
|

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.