Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
CORS explained: the same-origin policy, simple vs preflighted requests, the Access-Control headers, credentials rules and how to fix common CORS errors.
Quick answer: Browsers enforce a same-origin policy: JavaScript on one site cannot read responses from a different site. CORS (Cross-Origin Resource Sharing) is the mechanism a server uses to relax that rule for chosen sites, by sending response headers such as Access-Control-Allow-Origin. If your page at app.example.com calls api.example.com and the API does not send headers that permit your origin, the browser withholds the response and reports a CORS error. CORS is enforced by the browser, and it is configured on the server. It cannot be fixed from front-end code, and it does not block tools such as curl.
An origin is the combination of scheme, host and port. All three must match.
| URL | Same origin as https://app.example.com? |
|---|---|
https://app.example.com/settings | Yes: only the path differs |
http://app.example.com | No: different scheme |
https://api.example.com | No: different host |
https://app.example.com:8443 | No: different port |
https://example.com | No: different host |
Subdomains are separate origins. So are localhost:3000 and localhost:8080, which is why CORS errors are so common in development.
Browsers attach your cookies automatically to requests for the site that set them. Now imagine there were no restriction:
https://bank.example/accounts. Your browser includes your bank cookies.The same-origin policy stops step 4. A script may only read responses from its own origin.
One subtlety matters a great deal: for simple requests the policy blocks reading the response, not sending the request. The request still reaches the server. That is why protection against forged requests (CSRF) is a separate concern; see cookies vs localStorage vs sessions.
The policy has always allowed some cross-origin embedding: images, stylesheets, scripts and iframes. What it restricts is reading data from script, mainly through fetch and XMLHttpRequest.
Modern applications need cross-origin calls: a front end on one domain, an API on another. CORS lets the server say which origins may read its responses. MDN's CORS guide is the reference.
For basic requests, the browser sends the request with an Origin header:
GET /products HTTP/1.1
Host: api.example.com
Origin: https://app.example.com
The server replies with a header stating which origin may read the response:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://app.example.com
Content-Type: application/json
The browser compares. If the header matches the page's origin, or is *, the script gets the response. If it is missing or different, the browser blocks access and logs an error.
A request counts as "simple" only if it uses GET, HEAD or POST, sets no custom headers, and has a Content-Type of a form submission or plain text. These are the requests an ordinary HTML form could already make.
Anything else, such as PUT or DELETE, a JSON body, or an Authorization header, could have side effects on servers written before CORS existed. So the browser asks first with an automatic OPTIONS request called a preflight:
OPTIONS /orders/42 HTTP/1.1
Host: api.example.com
Origin: https://app.example.com
Access-Control-Request-Method: DELETE
Access-Control-Request-Headers: authorization, content-type
The server answers 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: authorization, content-type
Access-Control-Max-Age: 86400
Only if the answer permits it does the browser send the real request. Access-Control-Max-Age lets the browser cache the answer, so it does not preflight every call.
Note that sending JSON with Content-Type: application/json triggers a preflight. That surprises many people.
| Response header | Purpose |
|---|---|
Access-Control-Allow-Origin | Which origin may read the response: one specific origin, or * |
Access-Control-Allow-Methods | Which HTTP methods are permitted (preflight) |
Access-Control-Allow-Headers | Which request headers are permitted (preflight) |
Access-Control-Allow-Credentials | Whether cookies and credentials may be included |
Access-Control-Expose-Headers | Which response headers scripts may read |
Access-Control-Max-Age | How long to cache the preflight result |
By default, cross-origin fetch calls do not send cookies. To include them:
fetch("https://api.example.com/me", { credentials: "include" });
and the server must respond with:
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Credentials: true
Two strict rules apply:
Access-Control-Allow-Origin cannot be *. It must name the exact origin.Vary: Origin so caches do not serve one origin's response to another.Cookies also need suitable SameSite settings to be sent cross-site at all.
| Error | Likely cause | Fix |
|---|---|---|
| "No 'Access-Control-Allow-Origin' header is present" | The server sends no CORS headers | Configure CORS on the server for your origin |
| "...header has a value that is not equal to the supplied origin" | The allowed origin does not match exactly | Check scheme, host and port; no trailing slash |
| "Response to preflight request doesn't pass access control check" | The server does not handle OPTIONS | Respond to OPTIONS with the CORS headers and a success status, without requiring authentication |
| "Request header field authorization is not allowed" | Header missing from Access-Control-Allow-Headers | Add it |
| "...must not be the wildcard '*' when the request's credentials mode is 'include'" | Wildcard used with credentials | Return the specific origin and allow credentials |
Other things to check:
500 error, or a proxy returns an error page, the response has no CORS headers, and the browser reports CORS. Look at the Network tab for the actual status.OPTIONS itself.Most frameworks have middleware for this:
// Express
import cors from "cors";
app.use(cors({
origin: ["https://app.example.com"],
credentials: true,
}));
mode: "no-cors". It sends the request but gives you an opaque response that your code cannot read.If the browser never makes a cross-origin request, CORS never applies.
/api to the back end.localhost:3000, which forwards API calls.CORS protects users from malicious websites reading their data. It does not protect your API from anyone else.
Misconfiguration can open holes:
Origin back while allowing credentials lets every website read your users' data.null origin.example.com, which also matches evil-example.com.Use an explicit allow-list of origins.
The browser blocked your script from reading a response from a different origin because the server did not send headers allowing it.
CORS is enforced only by browsers. Other clients do not apply the same-origin policy.
An automatic OPTIONS request the browser sends before certain cross-origin requests, to ask the server whether the real request is allowed.
Access-Control-Allow-Origin: * safe?For truly public data with no credentials, yes. It must not be used for endpoints that rely on cookies or return private data.
A CORS error is the browser doing its job: refusing to hand one site's data to another site's script without the server's consent. The fix always lives on the server, in the form of the right Access-Control headers and a proper answer to OPTIONS. Decide which origins really need access, list them explicitly, and remember that CORS protects users, while authentication protects your API.