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

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

问题描述

我想在 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

Uncaught (in promise) SyntaxError: Unexpected token <在位置 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-TypeAccept 等于 application/json.

Add two headers Content-Type and Accept to be equal to 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 (in promise) SyntaxError: Unexpected token &lt;在 JSON 中的位置 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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