gpt4 book ai didi

javascript - react Js : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

转载 作者:行者123 更新时间:2023-12-03 13:00:02 26 4
gpt4 key购买 nike

我想在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-TypeAccept 以等于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com