JavaScript

Blocked by CORS policy: what the browser is actually protecting

CORS errors are reported by the browser and fixed on the server, and one of the most common ones is not a CORS problem at all but a server error in disguise. Here is what the browser is checking, how to read the message, and the five usual causes.

The new checkout talks to your API, and the console fills with red: Access to fetch at 'https://api.example.com/orders' from origin 'https://shop.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. The same request works in Postman. The browser is not being difficult. It is protecting your users, and the fix lives on the server.

What does "blocked by CORS policy" mean?

It means a page tried to read a response from a different origin, and that server's answer did not say the page was allowed to. Browsers apply the same-origin policy: a script on one origin cannot read data from another unless the other origin opts in. Cross-Origin Resource Sharing, CORS, is that opt-in, done with Access-Control-Allow-* response headers.

An origin is the scheme, the host and the port together. https://example.com, https://www.example.com, http://example.com and https://example.com:8080 are four different origins. A frontend on localhost:5173 calling an API on localhost:8000 is cross-origin too.

One point that saves a lot of confusion: for many requests, the server already received and processed the request before the browser blocked anything. CORS does not stop the request reaching your server. It stops your page from reading the answer.

What is a preflight request?

For requests that could change data in ways an old-fashioned form never could, the browser asks permission first. Sending JSON, using methods such as PUT or DELETE, or adding headers such as Authorization all trigger it. The browser sends an OPTIONS request, the preflight, naming the method and headers it intends to use. The server must answer with a success status and headers allowing them. Only then does the real request go out.

Diagram: a cross-origin request from a page on https://shop.example.com to an API on https://api.example.com. For a request with JSON or an Authorization header, the browser first sends an OPTIONS preflight asking which methods and headers are allowed. The server must answer with Access-Control-Allow-Origin naming the page's origin and the allowed methods and headers. Only then does the browser send the real request, and it lets the page read the response only if that response also carries Access-Control-Allow-Origin. If the API answers with an error page that has no CORS headers, the browser reports a CORS error and hides the real status.
The server always answers. CORS decides whether your page may read the answer.

In the Network tab, a preflight appears as a separate OPTIONS row just before your request. When the error message mentions "preflight", that row is the one to inspect.

What usually causes a CORS error?

1. A server error wearing a CORS costume. The API threw an exception and answered with a 500. The error page came from the framework's error handler or the web server, which did not add the CORS headers your normal responses carry, so the browser reports a CORS failure and hides the real status from your script. Check the status column in the Network tab before touching any CORS setting. If it says 500, the bug is in the API, and the 500 error guide shows where to find it.

2. The origin does not match exactly. The API allows https://example.com and the page runs on https://www.example.com, or on the staging domain, or on http. The match is exact, so every variant you serve needs to be on the list.

3. Credentials with a wildcard. A request that sends cookies, with credentials: 'include', cannot be allowed with Access-Control-Allow-Origin: *. The server must name the specific origin, send Access-Control-Allow-Credentials: true, and add Vary: Origin so caches keep answers for different origins apart.

4. The preflight is not handled. The OPTIONS request hits a route that does not exist, so it gets a 404 or 405, or it passes through authentication middleware that rejects it, since a preflight never carries credentials. If the preflight gets redirected, for example from http to https or to add a trailing slash, browsers can report "Redirect is not allowed for a preflight request".

5. The header is added twice. Both nginx and the application add Access-Control-Allow-Origin, and the browser rejects a response where the header "contains multiple values". Configure CORS in exactly one place.

How do you fix it properly?

Configure CORS once, in the application or in the web server, with an explicit list of the origins that should have access. Most frameworks have a standard way to do it: the cors package for Express, the config/cors.php file in Laravel, and the django-cors-headers package for Django, each with its own setting for the allowed origins. The middleware also answers preflights for you, which removes cause four.

Make sure error responses carry the same headers as successful ones, so a real error shows up as a real error. And consider whether you need cross-origin requests at all. Serving the API under the same origin as the site, for example at /api through a reverse proxy, removes CORS from the picture entirely.

Found something wrong in here? Tell me.

Checking API...
Checking AI Gateway...
Check status
⚠️ Setup Required