我想在react js中获取我的Json文件,为此我使用fetch
。但它显示错误
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
可能是什么错误,我不知道。我什至验证了我的 JSON。
handleGetJson(){
console.log("inside handleGetJson");
fetch(`./fr.json`)
.then((response) => response.json())
.then((messages) => {console.log("messages");});
}
我的 Json (fr.json)
{
"greeting1": "(fr)choose an emoticon",
"addPhoto1": "(fr)add photo",
"close1": "(fr)close"
}
最佳答案
添加两个 header Content-Type
和 Accept
以等于 application/json
。
handleGetJson(){
console.log("inside handleGetJson");
fetch(`./fr.json`, {
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then((response) => response.json())
.then((messages) => {console.log("messages");});
}
关于javascript - react Js : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37269808/