繁体   English   中英

使用jQuery访问JSON时格式错误

[英]Not well formed error while accessing JSON with jquery

因此,我有一个JSON文件,其内容希望访问并存储在数组中。 一切似乎都工作正常,但Firefox控制台出现错误。

Not Well Formed.

它指向我的JSON。 我在JSONLint上验证了JSON,并显示“有效JSON”。

我的jQuery是:

$(function(){


$('#test').click(function(event){

$.getJSON("myfile.json",function(data){

var items=[];

$.each(data, function(key, val){

items.push(key + "," + val[0].subject + "," + val[0].room);

});
alert('transfer successful');
console.log(items);

});

});

});

我的JSON看起来像这样:

{
"8": [ {"subject":"Signals","room":"L20"} ],
"9": [ {"subject":"something","room":"L20"} ]
}

在这里可能会注意到,此代码在按预期填充items数组的情况下运行良好。 但是我不明白为什么会收到Not well formed错误的错误。

如果未设置Content-Type,则在FireFox中会发生这种情况。 确保您为myfile.json请求返回正确的myfile.json (“ application / json”等)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM