簡體   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