小编典典

React Js:未捕获(在promise中)SyntaxError:JSON中位置0处的意外令牌<

reactjs

我想在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");});
}

我的杰森(fr.json)

{
  "greeting1": "(fr)choose an emoticon",
  "addPhoto1": "(fr)add photo",
  "close1": "(fr)close"
}

阅读 348

收藏
2020-07-22

共1个答案

小编典典

添加两个标题,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");});
}
2020-07-22