我想在REACTIVE js中获取我的Json文件,为此我使用fetch.但它显示了一个错误

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

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

My Json (fr.json)

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

推荐答案

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

Json相关问答推荐

在PowerShell中,如何获取数据对所在的JSON对象的名称

由于无效的UTF-8开始字节0xa0,JSON被拒绝,但编码似乎有效

Jolt-Json转换:通过引用标识符(而不是索引)设置值

Powershell ConvertFrom-Json 意外地从包含字符串的单个项目数组生成字符串而不是对象数组

如何使用 JOLT 使用输入数组中的值和层次 struct 中的其他字段创建数组

使用动态语言jQuery:根据匹配模式提取与其他值匹配的值

jq - 仅在键值对存在的地方打印值

Shell脚本空格转义

将具有多个级别的 json 读入 DataFrame [python]

在 Flutter 中将对象转换为可编码对象失败

使用 Jolt 使用键中的值创建带有硬编码键和值的 JSON 数组

如何使用 LINQ 在 C# 中重构对象?

错误字符串的长度超过了maxJsonLength属性设置的值

解析包含换行符的 JSON

在 JSON 字符串中react i18n 换行符

字符串格式 JSON 字符串给出 KeyError

设置对象不是 JSON 可序列化的

从 JSON 中 Select 不同的值

Backbone.js 模型与集合

是否有一个 PHP 函数只在双引号而不是单引号中添加斜杠