I have been playing with React using WebStorm and when I try and fetch from an API I keep getting the error below:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' 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.
To fix this, I originally installed the JetBrains Debug IDE Chrome Extension which provides an option to force CORS. When this is set to * or http://localhost:3000 I continue to get the CORS error (sporadically, occasionally it worked.) I've also tried installing the Chrome Extension Allow-Access-Control-Origin: * which fixed the error for a while, but when I restarted my computer I started getting the error again.
At this point, I started trying every combination possible with both plugins. I disabled one and tried the other, I switched the headers to use http://localhost:3000 and then http:///, etc. Again, it worked at times but later I would start getting the error again.
And just when I decided to record the error and post here, I realized that the response to the request being made shows the headers are present! Here is a recording of the request and its headers:
I can't figure this out and it only happens on localhost, when I load the URL in Chrome it works fine.
Anyone mind pointing me in the right direction? I'm sure it's related to my lack of knowledge of CORS