Skip to main content
Guide 6 min read

CORS Errors Explained: Why Your API Call Fails in the Browser

Your API works in Postman. It works with cURL. Then you call it from your web app and the console fills with red text about CORS. Your code didn’t change, and neither did the server. Welcome to CORS errors, the most confusing message in front end development.

Here’s the good news. CORS isn’t random. It follows a short set of rules, and once you see them, the fix usually takes minutes. This guide explains why your API call fails in the browser, which header is missing and how to debug it step by step.

What CORS is (and what it isn’t)

Browsers follow the same origin policy. An origin is the combination of scheme, host and port. So https://app.example.com and https://api.example.com are different origins. So are http and https on the same host, and so are two ports on localhost.

By default, a page can send a request to another origin, but your JavaScript can’t read the response. CORS, short for cross origin resource sharing, is the system that lets a server say, “I allow that origin to read my answers.” The server says it with response headers.

Here’s the part people miss. The server doesn’t block anything. The browser does. For many requests the server still does the work, and the browser then decides whether your code may see the result. That’s why the error lives in your console and often leaves no trace in your server logs.

Why it works in Postman and fails in your browser

Postman, cURL and your backend code don’t enforce CORS. Only browsers do, because only browsers run scripts from many different sites on behalf of a logged in user. So a request that succeeds in Postman proves the API works. It proves nothing about CORS. Our guide on REST API requests covers what a request carries.

That’s also why CORS isn’t a security wall for your API. Anyone can call your server without a browser. Real protection comes from authentication and authorization.

Simple requests and preflight requests

Browsers sort cross origin requests into two groups.

A simple request is a GET, HEAD or POST that uses only basic headers and a plain content type, like form encoded or plain text. The browser sends it right away and then checks the response headers.

Everything else gets a preflight. Sending JSON, adding an Authorization header or using PUT, PATCH or DELETE triggers it. Before the real call, the browser sends an OPTIONS request that asks, in effect, “May I send this method with these headers from this origin?” The server must answer with a success status and the right headers. If it doesn’t, the browser never sends your real request.

This explains a classic puzzle. Your GET works, but your POST with JSON fails. The JSON content type moved you from the simple group to the preflight group.

The headers that decide everything

A handful of response headers control the whole show. Real config uses a hyphen between the words in each name, so copy the exact spelling from your framework docs.

  • Access Control Allow Origin names the origin that may read the response. It holds one specific origin, or a wildcard for fully public APIs.
  • Access Control Allow Methods lists the methods the server accepts, such as GET, POST and DELETE. It appears on the preflight answer.
  • Access Control Allow Headers lists the request headers the server accepts, such as Authorization and the content type header.
  • Access Control Allow Credentials says whether the browser may send cookies or auth data. Its only useful value is true.
  • Access Control Max Age tells the browser how long to cache the preflight answer, so it doesn’t ask before every call.
  • Access Control Expose Headers lists extra response headers your JavaScript may read.
  • Vary with the value Origin tells caches that the answer changes with the requesting origin.

The most common CORS errors and fixes

Here are the errors you’ll see most, in rough order of how often they bite.

  • No allow origin header is present. The server never added the header. Add CORS support in your server or framework, and allow your exact front end origin.
  • The header is there but the origin doesn’t match. Compare scheme, host and port character by character. A trailing slash, http versus https, or www versus no www breaks the match. Our URL encoder decoder helps you read an encoded URL.
  • The preflight fails. The OPTIONS request returns 404, 405 or 401. Your router may not handle OPTIONS, or your auth layer may block it. Let OPTIONS through before authentication runs.
  • A redirect on the preflight. Browsers refuse redirects on a preflight. Make sure your API URL is the final one, with the right scheme and no trailing slash redirect. Our redirect checker shows where a URL really ends up, and our guide on debugging redirect chains goes deeper.
  • A request header isn’t allowed. You sent Authorization or a custom header the server didn’t list. Add it to the allowed headers.
  • Multiple values in the allow origin header. A proxy and your app both add it. Keep it in one place.
  • The error response has no CORS headers. Your server returns a 500 or 404 without the headers, so the browser shows a CORS error and hides the real problem. Check the Network tab for the true status.
  • The origin is null. You opened an HTML file straight from disk. Serve it from a local web server instead.

CORS and cookies: why the wildcard stops working

Sending cookies or other credentials changes the rules. If your request includes credentials, the server can’t answer with a wildcard origin. It must echo the exact origin and also send the allow credentials header set to true. The same rule blocks wildcards in the allowed headers and methods.

On the client, you turn credentials on with the credentials setting in fetch or the withCredentials flag in XMLHttpRequest. Cookies add one more catch. Browsers also apply SameSite rules, so a cookie from another site may never be sent. A bearer token in the Authorization header avoids many of those headaches. If you use tokens, check the expiry in our JWT decoder before you chase a CORS ghost, and read our guide on reading a JWT safely.

How to debug a CORS error in five minutes

Work through these steps in order:

  1. Read the exact console message. It names the missing or wrong header.
  2. Open the Network tab and find the failed request. Look for an OPTIONS request just before it.
  3. Check the status of the OPTIONS request. Anything other than a success status points to a server or routing problem.
  4. Read the response headers on that request and compare the allow origin value with the Origin header your browser sent.
  5. Confirm the real response. If your API returns a 500, fix that first. Our JSON formatter helps you read an error body.
  6. Retest from the browser after each change, with a hard refresh to skip cached preflight answers.
  7. Still stuck? Rebuild the call in our API request builder and compare the headers you send.

Remember, Postman won’t reproduce a CORS failure, because it ignores CORS.

Fixes you should not use

Search results are full of shortcuts. Skip these:

  • The no cors mode in fetch. It doesn’t fix anything. It hides the response, so your code can’t read it.
  • Browser extensions that turn CORS off. Fine for a quick local test, useless for your users.
  • Launching your browser with security switched off. It’s dangerous, and it hides the bug instead of solving it.
  • Public CORS proxies. A stranger’s server then sees your requests, tokens and data.
  • A wildcard origin on private APIs without thought. Allow named origins instead.

Do it right in development and production

In development, the cleanest fix is a proxy. Most dev servers, such as Vite and webpack, can forward calls from your page’s own origin to the API. The browser sees one origin, and CORS never starts.

In production, you have two good choices. Put the front end and API behind one domain with a reverse proxy, or configure CORS on the API with an allow list of exact origins. Keep the list in config, not in code. Add Vary for Origin when you echo the origin, so caches and CDNs don’t serve the wrong answer. Test the preflight too, not just the main call. Matching origins with a pattern? Try it in our regex tester first.

The short version

CORS errors come from the browser, not your server. The server must send the right headers, including the allow origin header that names your exact origin. Preflight requests need a success answer on OPTIONS, and credentials need an exact origin because a wildcard won’t work there. Debug in the Network tab, fix the server and skip the hacks.

Open your console on that failing call now. Find the OPTIONS request, read its status and you’ll likely know the cause in a minute.

Free browser based tools on Black Claaw to rebuild requests, trace redirects, read responses and decode tokens.

View all tools

Advertisement