javascript - 在 react 中获取总是返回(JSON 中位置 0 处的意外标记 R)

标签 javascript reactjs get fetch

考虑获取请求(React):

fetch(
  "Some url ...",
  {
    method: `GET`,
    headers: {
      Accept: "application/json"
    }
  }
).then(response => {
  if (response.ok) {
    response.json().then(json => {
      console.log(json);
    });
  }
});

执行获取时我总是得到:

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

知道如何解决这个问题吗?我无法从响应中获取数据。

最佳答案

感谢@AntonRusak,问题是 json 而不是 text:

 fetch(
      "Your-url",
      {
        method: `GET`,
        headers: {
          Accept: "application/text"
        }
      }
    ).then(response => {
      if (response.ok) {
        response.text().then(text=> {
          console.log(text);
        });
      }
    });

关于javascript - 在 react 中获取总是返回(JSON 中位置 0 处的意外标记 R),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58681996/

相关文章:

jquery $.post $.get 在使用网站 URL 时不起作用

javascript - 正则表达式仅返回找到的第一个结果

java - 如何使用 jhipster 生成带有 json 的 Rest get 服务?

javascript - C3js 图表线在 grunt 构建后隐藏

javascript - 从存储在 React 数据库中的字符串渲染 HTML

javascript - 我的ajax调用不起作用?

javascript - 将自定义字段(如电话号码)添加到 react-stripe-elements

javascript - Angular Carousel 无法与 ng-repeat 一起使用

reactjs - 元素类型无效 - React.js(在网络上找不到解决方案)

javascript - 使用 redux-saga 响应服务器端渲染