【问题标题】:JavaScript $.each json is not definedJavaScript $.each json 未定义
【发布时间】:2018-07-31 15:43:04
【问题描述】:

我有一串 JSON

msg = {d: "[{"ID":1,"IndentDate":"30/07/2018","EmpCode":"4000…er":"sef"}]

我希望追加我的表格,但它的显示行未定义

$.each(msg, function(i, row) {
  $("#autoTable").append("<tr><td>" + row['IndentDate'] + "</td><td>" + row['EmpCode'] + "</td></tr>");
})

我试过了

$.each(json, function (i, msg) {
  $("#autoTable").append("<tr><td>" + msg.IndentDate + "</td><td>" + msg.EmpCode + "</td></tr>");
})

也请帮忙

【问题讨论】:

  • 使用 console.log(row, i) 。你在$.each(msg, function(i, row) {里面得到了什么。
  • 看起来更像是一个 JavaScript 对象,而不是 JSON 格式的字符串。它看起来也无效,这里似乎有一个额外的" d: "[{。它还缺少一个右大括号}
  • 所以错误在你的 json 中。 msg = {d: "[{'ID':1,'IndentDate':'30/07/2018','EmpCode':'4000…er':'sef'}]"}。如果您在外部使用双引号,请在对象属性内使用单引号。
  • "EmpCode":"4000…er":"sef" 这也是没有意义的。
  • json 正确我无法正确粘贴

标签: javascript jquery json ajax html


【解决方案1】:

这东西有效..希望它也能帮助其他人..感谢大家的帮助

var jsonString = JSON.parse(msg.d);

jsonString.forEach(函数(行){ $("#autoTable").append("" + row.IndentDate + "" + row.EmpCode + " "); });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多