[英]A cross-origin error was thrown. react doesn't have access to the actual error object in development in react
i am trying to clone data from props to state inside constructor.我正在尝试将数据从道具克隆到构造函数内的 state。
this.state = {
showAttemptModal: false,
showProgressbar: true,
data: JSON.parse(JSON.stringify(this.props.data))
}
But getting cross-origin error.但是出现跨域错误。 Why this is happening.为什么会这样。
If you have setup your server to support CORS and still getting this error, most likely it is an issue with reactjs while processing response from the server.如果您已将服务器设置为支持 CORS 并且仍然出现此错误,则很可能是 reactjs 在处理来自服务器的响应时出现问题。 In my case it was happening while trying to parse invalid response into JSON.就我而言,它是在尝试将无效响应解析为 JSON 时发生的。
CORS error has nothing to do with cloning data from props to state. CORS 错误与将数据从 props 克隆到 state 无关。 You need to pass following property for same in your request header while fetching data from API:在从 API 获取数据时,您需要在请求 header 中传递以下属性:
{Access-Control-Allow-Origin:"*", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.