我想在 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"
}
原文由 iamsaksham 发布,翻译遵循 CC BY-SA 4.0 许可协议
添加两个标头
Content-Type
和Accept
等于application/json
。