简体   繁体   中英

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.

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. In my case it was happening while trying to parse invalid response into JSON.

CORS error has nothing to do with cloning data from props to state. You need to pass following property for same in your request header while fetching data from API:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM