我想在react js中获取我的Json文件,为此我正在使用fetch。但是显示错误
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");}); }
我的杰森(fr.json)
{ "greeting1": "(fr)choose an emoticon", "addPhoto1": "(fr)add photo", "close1": "(fr)close" }
添加两个标题,Content-Type并Accept等于application/json。
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");}); }