【问题标题】:A cross-origin error was thrown. react doesn't have access to the actual error object in development in react引发了跨域错误。 react 无权访问 react 开发中的实际错误对象
【发布时间】:2020-07-15 06:26:15
【问题描述】:

我正在尝试将数据从道具克隆到构造函数中的状态。

this.state = {
  showAttemptModal: false,
  showProgressbar: true,
  data: JSON.parse(JSON.stringify(this.props.data))
}

但是出现跨域错误。为什么会这样。

【问题讨论】:

标签: javascript reactjs react-native react-redux next.js


【解决方案1】:

CORS 错误与将数据从 props 克隆到 state 无关。 从 API 获取数据时,您需要在请求标头中传递以下属性:

{Access-Control-Allow-Origin:"*", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"}

【讨论】:

  • 不!这些是响应标头,在请求中没有位置。 (而且我们不知道 OP 存在什么样的跨域错误)。
【解决方案2】:

如果您已将服务器设置为支持 CORS,但仍然出现此错误,则很可能是 reactjs 在处理来自服务器的响应时出现问题。就我而言,它是在尝试将无效响应解析为 JSON 时发生的。

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 2018-07-14
    • 1970-01-01
    • 2021-02-19
    • 2018-11-10
    • 2020-01-31
    • 1970-01-01
    • 2021-12-31
    • 2021-05-14
    相关资源
    最近更新 更多