DEV Community

Volution Technology
Volution Technology

Posted on

That CORS error in Chrome: what's actually wrong, and how to keep working while it gets fixed

You open your front end, hit the API, and the console says:

Access to fetch at 'https://api.example.com/items' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The annoying part is that the request often did reach the server. Open the Network tab and you'll see a response. Chrome got it, looked at the headers, and refused to give it to your JavaScript.

Why it only breaks in the browser

curl and Postman work fine because they don't enforce the rule. The rule lives in the browser, but the permission lives in the server's response. Your page at localhost:3000 is a different origin from api.example.com, so the browser only hands over the answer if the response says your origin is allowed, usually with Access-Control-Allow-Origin.

What the real fix looks like

It's on the API side, and it's worth getting all of it right:

  • Access-Control-Allow-Origin with your front end's origin (or * for a public API that doesn't use cookies).
  • If the request isn't "simple" (a JSON Content-Type, an Authorization header, PUT or DELETE), Chrome sends an OPTIONS preflight first. That answer needs Access-Control-Allow-Methods and Access-Control-Allow-Headers, and it has to succeed.
  • With cookies or credentials you need Access-Control-Allow-Credentials: true, and the allowed origin has to be one named origin. A * won't work there.
  • The one that got me more than once: the CORS headers are only on the 200 response. When the API returns a 401 or 500, there are none, so you see "CORS error" instead of the actual error and chase the wrong problem.

But what if you can't change the API today?

Maybe it belongs to another team, or the fix is waiting on a deploy. You still want to keep building the page. For that, you can add the response header in your own browser, just while you test.

Chrome's DevTools local overrides can edit response headers, but only for the files you override, which doesn't help when you call an API over and over. A header-editing extension changes every matching response instead. The steps are the same in any of them:

  1. Create a profile, say "Local CORS".
  2. Add a header: direction response, operation set, name Access-Control-Allow-Origin, value http://localhost:3000.
  3. For a preflight, also set Access-Control-Allow-Methods and Access-Control-Allow-Headers.
  4. Set the domain to the API's domain, not your page's. The response comes from there.
  5. Reload, check the Network panel, and turn the profile off when you're done.

Two honest caveats. This only changes your browser, so it fixes nothing for anyone else and nothing on a real site. And because the header is added after the server has answered, a server that rejects the preflight with an error status can still fail. Look at both the preflight and the real request in the Network panel.

Other ways that are usually better

  • A dev proxy in your bundler's config, so the browser only talks to your own origin. Best for something you'll keep working on.
  • Have your back end call the API and your page call the back end.
  • Chrome with web security turned off works, but it turns off protection for everything in that window. I'd skip it.

What's the CORS gotcha that cost you the most time? I'd like to hear it.

Disclosure: we make a header-editing extension (Header Switch), which is the kind of tool described above. I haven't tested these steps against your API, so check your own Network panel.

Top comments (0)