gpt4 book ai didi

javascript - 使用 JSON.parse 时出现无效字符 javascript 错误

转载 作者:行者123 更新时间:2023-11-28 07:30:06 25 4
gpt4 key购买 nike

我正在尝试将 json 字符串转换为从 .js 文件读取的对象格式。

下面是 document.js 中的 JSON 字符串

    [
{
"type": "TableShape",
"id": "63c0f27a-716e-804c-6873-cd99b945b63f",
"x": 80,
"y": 59,
"width": 99,
"height": 107,
"name": "Group",
"entities": [
{
"text": "id",
"id": "49be7d78-4dcf-38ab-3733-b4108701f1"
},
{
"text": "employee_fk",
"id": "49be7d78-4dcf-38ab-3733-b4108701fce4"
}
]
}
];

现在我正在使用AJAXwindow load中调用document.js,如下所示

 $(window).load(function () {
$.ajax({
url: "JS/Draw2d/SampleData/document.js",
async: false,
success: function (result) {
debugger;
jsonStringFromServer = JSON.parse(result);//Here Javascript error stating invalid character
alert(jsonStringFromServer);
}
});
});

最佳答案

$.ajax函数接收到JSON时,它会自动为您反序列化。您看到的错误是因为您将对象传递给 JSON.parse,而不是 JSON 格式的字符串 - 您不需要使用 JSON.parse > 完全没有。试试这个:

success: function (result) {
debugger;
console.log(result); // = the received object
}

我还强烈建议您删除 async: false,因为使用它是非常糟糕的做法。

关于javascript - 使用 JSON.parse 时出现无效字符 javascript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com