使用javascript和json.stringify函数附加json数据

时间:2014-07-17 09:20:53

标签: javascript jquery html eval

$.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>')

    });           
});

上面是我以json格式获取数据的函数,但我无法使用for循环和foreach循环追加。它显示未定义。如何使用for循环在表中附加数据?

这是JSON.stringify(data)

的结果
  

{&#34; Dpurchaseinvoice&#34;:[{&#34; pInvoiceDate&#34;:&#34; /日期(1405708200000)/&#34;&#34; pSupplierInvoiceNumber&#34;:&# 34; G003&#34;&#34; PTYPE&#34;:1},{&#34; pInvoiceDate&#34;:&#34; /日期(1405708200000)/&#34;&#34; pSupplierInvoiceNumber&# 34;:&#34; H008&#34;&#34; PTYPE&#34;:1}]&#34;的ErrorMessage&#34;:&#34;&#34;&#34; MID&#34 ;:0,&#34; mJobID&#34;:空,&#34; mSupplierID&#34;:空,&#34; mInvoiceDate&#34;:&#34; /日期(-62135596800000)/&#34; &#34; mOurRef&#34;:0,&#34; mSupplierInvoiceNumber&#34;:空,&#34; mPurchaseOrderRef&#34;:空,&#34; MTYPE&#34;:0,&#34; mPaid& #34;:空,&#34; mReferencePurchaseInvoiceID&#34;:空,&#34;供应商&#34;:空,&#34;的JobID&#34;:空,&#34; ID&#34;:0, &#34;供应商ID&#34;:空,&#34; InvoiceDate&#34;:&#34; /日期(-62135596800000)/&#34;&#34; OurRef&#34;:0,&#34 ; SuppliersInvoiceNumber&#34;:空,&#34; PurchaseOrderRef&#34;:空,&#34;类型&#34;:0,&#34;付费和#34;:空,&#34; ReferencePurchaseInvoiceID&#34 ;:空,&#34; ErrorSummary&#34;:空,&#34; ErrorList&#34;:[]}

1 个答案:

答案 0 :(得分:1)

您的代码有效。也许选择器#Gridinvoice是错误的。

编辑:这是您的代码,已更新为使用JSON数据:

$.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>')
});

请注意data.Dpurchaseinvoice和前缀item.pXXX