javascript - 我在解析 JSON 时遇到错误

标签 javascript jquery json ajax

我有这个示例 JSON 代码,我想用 jQuery AJAX 循环它,但由于某种原因,控制台上总是出错

这是 new_content.json 文件中的代码

[
    {
        "name": "Mehmet",
        "email": "mehmet@gmail.com"
    },
    {
        "name": "Fareed",
        "email": "fareed3242@yahoo.com"
    },
    {
        "name": "Jonathan",
        "email": "jonathan@gmail.com"
    }
]

这些是我的html

<div id="main">This is the original text when page loads</div>

<button id="button1" type="button">Update content with Ajax</button>

这是在 javascript 中

$(document).ready(function () {
    $("#button1").on('click', function () {
        $.ajax({
            type: "GET",
            url: "new_content.json",
            success: function (rawData) {
                var data = JSON.parse(rawData);
                var result = '';
                result += '<table> \
                                 <tr> \
                                        <th>name</th> \
                                        <th>email</th> \
                                    </tr> \
                                    <tr>';
                $.each(data, function (i, item) {
                    result += "<tr><td>" + item[i].name + "</td>";
                    result += "<td>" + item[i].email + "</td></tr>";
                });
                result += "</tr></table>";
                $("#main").html(result);
            }
        });
    });
});

最佳答案

有2个错误

  1. 首先删除 JSON.parse() 你不需要解析它。
  2. 迭代数据 item[i].name 不正确,它应该是 item.nameitem.email

除了上面列出的 2 个之外,如果这不是拼写错误,您也缺少 .each 的右括号。

运行下面的命令,一切都会好起来的,我刚刚在我的本地机器上确认了

$(document).ready(function () {
        $("#button1").on('click', function () {
            $.ajax({
                type: "GET",
                url: "my.json",
                success: function (rawData) {
                    var data = rawData;

                    var result = '';
                    result +=
                        '<table> \
                                <tr> \
                                        <th>name</th> \
                                        <th>email</th> \
                                    </tr> \
                                    <tr>';
                    $.each(data, function (i, item) {
                        result += "<tr><td>" + item.name + "</td>";
                        result += "<td>" + item.email + "</td></tr>";
                    });
                    result += "</tr></table>";
                    $("#main").html(result);
                }
            });
        });
    });

关于javascript - 我在解析 JSON 时遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47581837/

相关文章:

javascript - 使用 jasascript 或 angularjs 为 google map 创建动态 url 表达式

javascript - async.waterfall 从不执行第二个函数

javascript - 如何在每个具有新数据属性的元素之前插入元素

php - json_encode 从键中删除引号?

c# - 如何使用c#读取和显示文本文件中的json数据

javascript - 类型 'current' 上不存在属性 '((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement>'

javascript - 从 javascript 更改 html 中的字体和值

javascript - 处理代码时锁定整个页面

jQuery 自定义滚动条脚本不工作

c# - 如何将字符串列表(已经在单独的 json 数据中)转换为单个 json 数组导致 c#?