我试图通过以下方式获取JSON文件:
$.ajax('/file.json', {
contentType: 'application/json',
dataType: 'jsonp',
success: function (data) {
console.log(data);
},
error: function (jqXHR, text, errorThrown) {
console.log(jqXHR + " " + text + " " + errorThrown);
}
});
但是,我总是得到这个错误:
parsererror SyntaxError: Unexpected token :
我的JSON文件非常简单:
{
stuff: "some stuff"
}
我已尝试过所有内容,我听说过一些跨域内容,但JSON文件位于html的同一目录中。我不知道如何解决它。
答案 0 :(得分:3)
将stuff:
更改为"stuff":
这是正确的json语法。
答案 1 :(得分:0)
尝试将其更改为:
$.ajax({
url: 'file.json',
contentType: 'application/json',
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (jqXHR, text, errorThrown) {
console.log(jqXHR + " " + text + " " + errorThrown);
}
});
但看起来可能会返回一些糟糕的JSON。尝试使用您确定有效的其他JSON文件吗?
此外,如果file.json直接相同,则在file.json之前包含'/'。除非您是从虚拟主机或Web服务器根目录运行它
答案 2 :(得分:0)
更改您的
dataType: 'jsonp'
到
dataType: 'json'