【问题标题】:Append json data using javascript and json.stringify function使用 javascript 和 json.stringify 函数附加 json 数据
【发布时间】:2014-08-18 17:02:26
【问题描述】:
$.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)的结果

{"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,"类型":0,"Paid":null,"ReferencePurchaseInvoiceID":null,"ErrorSummary":null,"ErrorList":[]}

【问题讨论】:

  • 欢迎来到 Stack Overflow。您可以使用Code Sample {} 工具栏按钮格式化源代码——这次我已经为您完成了。
  • 您能否也发布从“suppliermanagement.ashx”返回的示例数据?
  • 可能返回data不是json格式
  • 返回的数据必须是 Object {} 或 Array[] 的形式。如果您输入的数据内部有更多信息,您必须先检索该信息。假设您要返回一个带有 arrayField 的 Object,其中包含您实际想要的数据,您需要将 data.arrayField 输入到 .each 循环中。

标签: javascript jquery html eval


【解决方案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

【讨论】:

  • "#Gridinvoice" 是表 ID。我在该表上附加行。我附加了行,但我无法在行中获得完美的数据。数据在“td”中未定义。如果我不使用 for 循环或 $.each,我只能从数据库中获取一行。
  • 使用“console.log”而不是“alert”。打开控制台(在 Chrome 或 Firefox 中为 CTRL + SHIFT + I),将“JSON.stringify(data)”的打印内容复制到此处。
  • @user2835692,请贴出从服务器获取的数据,否则只能假设..
  • @user2835692,请粘贴“console.log(JSON.stringify(data));”的结果。不要编辑我的帖子(但你可以编辑你的!)并且不要忘记“JSON.stringify”。
  • @Tarh 我已将 Json.Stringify 附加到我的帖子中。
猜你喜欢
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
相关资源
最近更新 更多