gpt4 book ai didi

ajax - 使用AJAX读取语法错误JSON文件

转载 作者:行者123 更新时间:2023-12-03 08:06:42 25 4
gpt4 key购买 nike

我从JSON文件中收到方括号'['的语法错误。下面给出的是JSON。

[

{
"Product Name":"CPE 10281",
"Application":"Tyres",
"Weight":150,
"Cost to Produce":5000,
"Grade":"A",
"Cost to Sell":40000,
"Strength":100
},
{
"Product Name":"CPE 10282",
"Application":"computers",
"Weight":250,
"Cost to Produce":4000,
"Grade":"H",
"Cost to Sell":25000,
"Strength":90
}
]

我正在尝试使用AJAX读取JSON文件。
                $.ajax({
url: "dataProductJSON.json",
dataType: 'json',
mimeType: "application/json",
success: function (data) {
var item = [];
$.each(data, function (key, val) {
item.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'interest-list',
html: item.join('')
}).appendTo('body');
},

});

我正在使用Apache Geronimo作为服务器从Eclipse运行html。
请帮忙。

最佳答案

您在下面的行中缺少{

success: function (data) 

做了
success: function (data) {

编辑

您解析的数据不正确,请按照以下说明进行操作
     $.ajax({
url: "test.html",
dataType: 'json',
mimeType: "application/json",
success: function (data) {
var item = [];
$.each(data, function (key, val){
$.each(val, function (innerKey, innerValue){
item.push('<li id="' + innerKey + '">' + innerValue + '</li>');
});
});
$('<ul/>', {
'class': 'interest-list',
html: item.join('')
}).appendTo('body');
},

});

您需要使用2个循环,一个用于数组,另一个用于遍历object属性

我尝试了一些东西,但效果很好

关于ajax - 使用AJAX读取语法错误JSON文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076210/

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