简体   繁体   English

使用jQuery访问JSON时格式错误

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

So I have a JSON file whose contents I wish to access and store in an array. 因此,我有一个JSON文件,其内容希望访问并存储在数组中。 Everything seems to be working fine but I am getting an error in Firefox console. 一切似乎都工作正常,但Firefox控制台出现错误。

Not Well Formed.

and it is pointing to my JSON. 它指向我的JSON。 I validated my JSON at JSONLint and it says "Valid JSON". 我在JSONLint上验证了JSON,并显示“有效JSON”。

My jquery is: 我的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);

});

});

});

My JSON looks something like this: 我的JSON看起来像这样:

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

It may here be noted that this code is working fine with the items array getting populated as expected. 在这里可能会注意到,此代码在按预期填充items数组的情况下运行良好。 But I cannot understand why I am getting the Not well formed error. 但是我不明白为什么会收到Not well formed错误的错误。

This happens in FireFox when Content-Type is not set. 如果未设置Content-Type,则在FireFox中会发生这种情况。 Ensure you are returning the correct mimetype for your myfile.json request ("application/json" etc.) 确保您为myfile.json请求返回正确的myfile.json (“ application / json”等)

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

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