React Js:Uncaught(在promise中)SyntaxError:意外的标记<在位置0的JSON中 [英] React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

查看:179
本文介绍了React Js:Uncaught(在promise中)SyntaxError:意外的标记<在位置0的JSON中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在react js中获取我的Json文件,为此我使用 fetch 。但它显示错误

I want to fetch my Json file in react js, for this I am using fetch. But it shows an error

未捕获(在承诺中)SyntaxError:意外的令牌<在位置0的JSON中

可能是错误,我不知道任何线索。我甚至验证了我的JSON。

What could be the error, i am getting no clue. I even validated my JSON.

handleGetJson(){
  console.log("inside handleGetJson");
  fetch(`./fr.json`)
    .then((response) => response.json())
    .then((messages) => {console.log("messages");});
}

我的Json(fr.json)

My Json (fr.json)

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


推荐答案

添加两个标题 Content-Type 接受等于 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");});
}

这篇关于React Js:Uncaught(在promise中)SyntaxError:意外的标记&lt;在位置0的JSON中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆