【问题标题】:React fetch giving me this error: Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0React fetch 给我这个错误: Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0
【发布时间】:2018-03-22 18:48:25
【问题描述】:

我已将 create-react-app 中的代理设置为根 URL,如下所示:

"proxy": "https://od-api.oxforddictionaries.com/api/v1",

所以没有 CORS 问题...

然后我执行一个简单的获取并传入包含内容类型和接受设置为 application.json 的标头

componentDidMount() {
    fetch('/entries/en/ace', {
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        app_id: 'API_ID',
        app_key: 'APP_KEY',
      },
    })
      .then(response => response.json())
      .then(data => {
        console.log('====================================');
        console.log(data);
        console.log('====================================');
      });
  }

似乎在 Postman 中工作得很好......只需设置标题,发送获取请求并获取响应......我可以使用 chrome 扩展来完成它,它工作正常......我错过了什么?

【问题讨论】:

  • 您确定收到 JSON 作为响应吗?
  • 是的,我只是重写了完全相同的东西。我现在正在比较它 - 相同 - 它有效.... ??????????‍♂️

标签: reactjs fetch


【解决方案1】:

您是否尝试调试第一个“then”子句? 我还建议不要使用 fetch,而是使用 axios - https://www.npmjs.com/package/axios

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2021-01-10
    • 2021-05-29
    • 2021-09-30
    • 2022-01-22
    • 2019-08-14
    相关资源
    最近更新 更多