CORS explained: why the browser blocked your request and how to fix it
CORS errors are frustrating because the request often works perfectly from the command line and fails only in the browser. That is not a bug. It is the clue to what CORS is: a rule that browsers enforce on behalf of their users, which a server can choose to relax.
The rule being relaxed: the same-origin policy
An origin is the combination of scheme, host and port. These are four different origins:
https://example.com
http://example.com ← different scheme
https://api.example.com ← different host
https://example.com:8443 ← different port
By default, a script running on one origin may send requests to another, but may not read the responses. The reason is that your browser attaches your cookies to requests automatically. Without this rule, any page you visited could quietly request your webmail or your bank and read what came back, as you.
Cross-Origin Resource Sharing is the mechanism by which a server says "responses from me may be read by scripts from these other origins". The server states the policy in response headers; the browser enforces it.
That explains the command-line puzzle. A tool like curl is not a browser, carries nobody's cookies and enforces nothing. It follows that CORS is not access control for your API. It protects users of browsers. It does not stop anyone from calling your server directly, so it is never a substitute for authentication.
Simple requests and preflighted requests
Browsers divide cross-origin requests into two kinds.
A simple request is one that an HTML form could already have made before CORS existed: a GET, HEAD or POST with only basic headers and a content type of text/plain, multipart/form-data or application/x-www-form-urlencoded. The browser sends it directly, then checks the response headers to decide whether the script may see the result.
Anything else triggers a preflight. That includes most API calls, because sending Content-Type: application/json, using PUT, PATCH or DELETE, or adding a header such as Authorization all take a request out of the simple category. Before the real request, the browser asks permission with an OPTIONS request:
OPTIONS /orders HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, authorization
The server must answer with what it allows:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: content-type, authorization
Access-Control-Max-Age: 600
Only if this answer covers the method and every header does the browser send the real request. Note that a simple request is delivered to your server even when the response is then hidden from the script. CORS controls reading, not sending, which is why it is not a defence against cross-site request forgery.
What each header does
| Response header | Purpose |
|---|---|
Access-Control-Allow-Origin | The one origin allowed to read the response, or * for any |
Access-Control-Allow-Methods | Methods permitted, in the preflight answer |
Access-Control-Allow-Headers | Request headers permitted, in the preflight answer |
Access-Control-Allow-Credentials | true if cookies or HTTP authentication may be included |
Access-Control-Expose-Headers | Response headers the script may read beyond the basic set |
Access-Control-Max-Age | Seconds the browser may cache the preflight answer |
Access-Control-Allow-Origin takes a single origin or a wildcard. It does not take a list. To allow several origins, the server compares the incoming Origin header against its own allow-list and echoes back the matching value. When it does that, it must also send Vary: Origin, or a cache may serve one origin's response to another.
Cookies change the rules
If the request includes credentials (the script sets credentials: "include" in fetch), three stricter requirements apply:
Access-Control-Allow-Originmust name the exact origin. The wildcard is rejected.Access-Control-Allow-Credentials: truemust be present.- Wildcards are not accepted in the allowed headers or methods either.
This is the most common "it works without cookies but not with them" failure. The tempting shortcut is to echo back whatever Origin arrives while also allowing credentials. That makes every website on the internet a trusted reader of your users' authenticated data, which is exactly what the same-origin policy exists to prevent. Always check against an explicit list.
Reading the error message
The browser console tells you which check failed. The common ones:
- "No 'Access-Control-Allow-Origin' header is present": the server sent no CORS headers on this response. Often the response is an error page from a proxy or framework that bypassed your CORS code. Look at the status code first.
- "Response to preflight request doesn't pass access control check": the OPTIONS request failed. Frequently the server requires authentication on OPTIONS, which can never succeed because browsers do not send credentials with a preflight, or the route simply does not handle OPTIONS.
- "Request header field x is not allowed": add that header to
Access-Control-Allow-Headers. - "The value of 'Access-Control-Allow-Origin' must not be the wildcard when credentials mode is 'include'": the cookie rule above.
The script itself cannot see any of this. It receives only a generic network error, by design, so that it learns nothing about the other origin. Always debug CORS from the browser's network tab, where the preflight and its headers are visible, or reproduce the preflight in a CORS tester.
Fixes that work, and fixes that only appear to
- Fix it on the server. CORS is the server's decision. Nothing in the page's JavaScript can grant itself access.
- Handle OPTIONS before authentication and return the CORS headers on error responses too, or every failure will be reported as a CORS problem and hide the real cause.
- Avoid the problem by serving the API from the same origin as the page, for example under
/apibehind a reverse proxy. In development, the dev server's proxy setting does the same job. mode: "no-cors"is not a fix. It lets the request go out but gives the script an opaque response it cannot read. It is for cases like caching a third-party image, not for calling an API.- Browser extensions and flags that disable CORS change your browser only. Your users will still be blocked.
- Public proxy services that add CORS headers see every request and response that passes through them. Do not send tokens or personal data through one.
A sensible default configuration
- Keep an explicit list of allowed origins per environment. No wildcard for anything that uses cookies or returns private data.
- Echo the matching origin and send
Vary: Origin. - Allow only the methods and headers you use.
- Set
Access-Control-Max-Ageto several minutes so that each request is not preceded by a preflight. - Use the wildcard only for truly public, read-only resources such as open data or fonts.