我正在使用ES6和Fetch。
它给了我这个错误:
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
以下是代码:
fetch('list.json').then(function(response) {
return response.json();
}).then(function(j) {
console.log(j);
});
这里是来自list.json的数据:
[
{
"id": 1,
"name": "name1"
},
{
"id": 2,
"name": "name2"
}
]
为什么我收到此错误,如何解决此问题?
答案 0 :(得分:0)
这不是有效的JSON文件。有效的JSON就像这样
{
"data":[
{
"id": 1,
"name": "name1"
},
{
"id": 2,
"name": "name2"
}
]
}
答案 1 :(得分:0)
你没有发送正确理解.json方法的JSON,你应该以上面提到的方式进行转换,一切都应该好了。
更正JSON:
{ "products" : [
{ "Name": "Cheese", "Price" : 2.50, "Location": "Refrigerated foods"},
{ "Name": "Crisps", "Price" : 3, "Location": "the Snack isle"},
{ "Name": "Pizza", "Price" : 4, "Location": "Refrigerated foods"},
{ "Name": "Chocolate", "Price" : 1.50, "Location": "the Snack isle"},
{ "Name": "Self-raising flour", "Price" : 1.50, "Location": "Home baking"},
{ "Name": "Ground almonds", "Price" : 3, "Location": "Home baking"}
]}