Wednesday, February 27, 2019

javascript - Enable CORS in fetch api

I am getting the following error message that unable me to continue




Failed to load https://site/data.json: Response to preflight request
doesn't pass access control check: No 'Access-Control-Allow-Origin'

header is present on the requested resource. Origin
'http://localhost:8080' is therefore not allowed access. If an opaque
response serves your needs, set the request's mode to 'no-cors' to
fetch the resource with CORS disabled.
localhost/:1 Uncaught (in promise) TypeError: Failed to fetch




I am trying to enable CORS in my react js file but I was not able to get the expected result. I have installed a chrome extension and it work. But to use this in production site, I need to enable it inside my code. How should I properly arrange the code to enable the CORS.



fetch(URL, {

mode: 'cors',
headers: {
'Access-Control-Allow-Origin':'*'
}
})
.then(response => response.json())
.then(data => {

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...