A CORS error means the browser refused to let your JavaScript read a response from another origin (a different domain, protocol or port). The request often reached the server and even succeeded there: the browser just hides the response from your code because the server didn’t send the right Access-Control-* headers.
This guide explains how to read a CORS error, how to find the real cause in a minute with the browser’s Network tab or curl, and the exact fix for every common CORS error message. It ends with tested fixes for Express, Laravel and Django, links to our step-by-step guides for Nginx, Apache, Amazon S3 and Firebase Storage, and the safe way to avoid CORS during local development.
What a CORS Error Actually Means
Two URLs have the same origin only if the scheme, host and port all match. https://app.example.com and https://api.example.com are different origins, and so are http://localhost:3000 and http://localhost:5173.
When a page calls another origin with fetch() or XHR:
- The browser adds an
Originheader to the request. - For “non-simple” requests (PUT, DELETE, PATCH, JSON bodies, an
Authorizationheader…), it first sends a preflightOPTIONSrequest asking whether the real request is allowed. - Only if the response contains an
Access-Control-Allow-Originheader that matches the page’s origin does the browser hand the response to your code.
Three consequences that explain most confusion:
- The fix is always on the server that sends the response (or a proxy in front of it), never in your frontend code. Adding
Access-Control-Allow-Originas a request header does nothing. - curl and Postman don’t enforce CORS. “It works in Postman” is normal and proves nothing.
- CORS doesn’t block the request itself. In our tests, a GET from a disallowed origin still returned
200 OKwith the data on the server side; only the browser refused to expose it. CORS is not a replacement for authentication or CSRF protection.
Debug Any CORS Error in 3 Steps
Step 1: Read the whole message in the console
The part after “has been blocked by CORS policy:” names the exact problem. Match it with the error list below.
Step 2: Look at the failing request in the Network tab
Open the developer tools (F12), go to Network, reload, and click the red request. Check:
- Is there a separate OPTIONS (preflight) request before it, and what status did it get? It must be 2xx.
- What is the real status code? A 401, 403, 404 or 500 without CORS headers shows up in the console as a CORS error, even though the real problem is authentication, a wrong URL or a server crash.
- Which
Access-Control-*headers are in the response?
Step 3: Reproduce it with curl
Send the same preflight the browser sends, with your page’s origin:
curl -i -X OPTIONS https://api.example.com/items/1 -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: PUT" -H "Access-Control-Request-Headers: content-type, authorization"
and the real request:
curl -s -o /dev/null -D - https://api.example.com/items -H "Origin: https://app.example.com"
A working API answers the preflight with a 2xx status and headers like these (from our Express test):
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Vary: Origin, Access-Control-Request-Headers
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Headers: content-type,authorization
Access-Control-Max-Age: 86400
Always include the Origin header: most servers only send CORS headers when the request has one.
Common CORS Errors and How to Fix Them
These are the messages Chrome and Edge show; Firefox uses similar wording with a “Reason:” line.
1. No ‘Access-Control-Allow-Origin’ header is present on the requested resource
The response has no allow header at all. Causes, from most to least common:
- CORS isn’t configured on the server, or not for this path. In Laravel, for example, only paths listed in
config/cors.phpget CORS headers; in our test a route outsideapi/*returned none. - Your page’s origin isn’t in the allowed list. Compare it character by character:
httpvshttps,www, and the port. - The response is an error (404, 500, 502) and the server only adds headers to successful responses. Use
alwaysin Nginx (add_header … always) andHeader always setin Apache. - A CDN or cache served a copy cached without CORS headers; add
Vary: Originand purge the cache.
2. The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *’, but only one is allowed
Two layers each added the header, typically the application and the web server or proxy in front of it, or the header was configured with a comma-separated list of domains. Configure CORS in only one place. Behind a proxy, remove the backend’s copy (proxy_hide_header in Nginx, Header unset in Apache). To allow several domains, echo back the single matching origin; see allowing multiple origins.
3. Response to preflight request doesn’t pass access control check: It does not have HTTP ok status
The OPTIONS request got a 401, 403, 404, 405 or 500. Usually authentication middleware rejects it (browsers never send credentials with a preflight), or the framework has no route for OPTIONS. Let preflight requests through before authentication, or answer them at the web server with 204.
4. Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response
Add the named header (Authorization, Content-Type, X-Requested-With, X-API-Key…) to Access-Control-Allow-Headers. Note that Content-Type: application/json by itself makes a request non-simple and needs Content-Type in this list.
5. Method PUT is not allowed by Access-Control-Allow-Methods in preflight response
Add the method to Access-Control-Allow-Methods.
6. The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’
Your frontend sends cookies or HTTP auth (credentials: 'include', Axios withCredentials: true), and the server answers with *. With credentials, the server must return the exact origin and Access-Control-Allow-Credentials: true. Either allow a specific origin list, or drop credentials from the request if you don’t need cookies.
7. The value of the ‘Access-Control-Allow-Credentials’ header in the response is ” which must be ‘true’
The request includes credentials but the response lacks Access-Control-Allow-Credentials: true. Add it, on both the preflight and the real response. Firefox reports this as “CORS No Allow Credentials”.
8. The ‘Access-Control-Allow-Origin’ header has a value ‘https://a.example.com’ that is not equal to the supplied origin
The server returns a fixed origin that isn’t yours, or a cache served a response made for another origin. Echo the request’s origin from an allow list and send Vary: Origin.
9. Redirect is not allowed for a preflight request
The preflight was redirected: http → https, a missing or extra trailing slash (/api/items → /api/items/), or a login redirect. Call the final URL directly from your frontend.
10. Origin ‘null’ / cross origin requests are only supported for protocol schemes http, https…
You opened the HTML file directly from disk (file://). Serve it over HTTP instead, for example with python3 -m http.server 8080 in its folder, or your framework’s dev server.
11. CORS request did not succeed / net::ERR_FAILED
The request never got an HTTP response: the server is down, the port is wrong, DNS fails, the TLS certificate is invalid, or a browser extension or firewall blocked it. Open the API URL directly in a browser tab to see the underlying error.
Fix CORS in Your Server or Platform
Detailed, tested guides for the most common servers and storage services:
- Nginx:
add_header … always, multiple origins withmap, preflight, proxies, and theadd_headerinheritance trap. - Apache: mod_headers,
SetEnvIforigin lists,.htaccess, preflight with mod_rewrite and credentials errors. - Amazon S3: bucket CORS rules in the console, CLI and Terraform, uploads, CloudFront.
- Express (Node.js): the cors middleware, credentials, preflight and Express 5 changes.
- Firebase Storage:
cors.jsonwithgcloud storage buckets update.
For applications, configure CORS in the framework. These snippets were tested with the versions shown.
Express (Node.js), with the cors package
Full guide: how to enable CORS in Express (credentials, per-route CORS and the Express 5 app.options('*') error).
npm install cors
const express = require('express');
const cors = require('cors');
const app = express();
app.use('/api', cors({
origin: ['https://app.example.com', 'http://localhost:5173'],
credentials: true,
maxAge: 86400,
}));
Tested with Express 5.3 and cors 2.8.6: allowed origins are echoed back with Vary: Origin, preflights are answered with 204, and other origins get no allow header. Calling cors() without options allows every origin with *.
Laravel 11, 12 and 13
Laravel handles CORS itself with the built-in HandleCors middleware, but since Laravel 11 the config file isn’t there until you publish it:
php artisan config:publish cors
Then edit config/cors.php:
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['https://app.example.com'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
Only URLs matching paths get CORS headers: add other prefixes there if your frontend calls them. Set supports_credentials to true for Sanctum cookie authentication, and run php artisan config:clear after changes. Tested on Laravel 13.44.
Django, with django-cors-headers
pip install django-cors-headers
In settings.py:
INSTALLED_APPS = [
# ...
"corsheaders",
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware", # as high as possible
"django.middleware.common.CommonMiddleware",
# ...
]
CORS_ALLOWED_ORIGINS = ["https://app.example.com"]
CORS_ALLOW_CREDENTIALS = True
CorsMiddleware must come before CommonMiddleware and any middleware that can return a response early. Tested with Django 6.1 and django-cors-headers 4.9.
Allow Multiple Origins Correctly
Access-Control-Allow-Origin holds exactly one origin or *. To allow several sites, the server must compare the request’s Origin with an allow list and send back that single origin, plus Vary: Origin so caches keep responses for different origins apart. All the framework packages above do this when you give them a list; in Nginx use a map, and in Apache SetEnvIf, as shown in their guides.
Avoid CORS in Local Development with a Dev Proxy
When your frontend dev server (localhost:5173) calls a local or remote API, let the dev server proxy the API instead of changing CORS settings. The browser then talks to a single origin and no CORS check happens. In Vite, vite.config.js:
export default {
server: {
proxy: {
'/api': 'http://localhost:3000',
},
},
};
Then call fetch('/api/items') in your code, without the host. In our test with Vite 8.4, a request to http://localhost:5173/api/items was forwarded to the API on port 3000 and returned its data. webpack-dev-server and Angular have the same feature (devServer.proxy and proxy.conf.json). In production, serving the frontend and API under one domain with a reverse proxy has the same effect.
Avoid “fixes” that only hide the problem: browser extensions that disable CORS, launching Chrome with --disable-web-security, or public CORS proxy services. They don’t work for your users, and public proxies can see every request.
Frequently Asked Questions
Can I fix a CORS error in my frontend JavaScript?
No. The browser checks the headers the server sends. You can only change the server’s configuration, put a proxy you control in front of it, or call it from your own backend instead of the browser.
Why does the API work in Postman or curl but not in the browser?
Only browsers enforce CORS. Postman and curl ignore the Access-Control-* headers completely.
Why do I see an OPTIONS request I never sent?
That is the preflight. The browser sends it automatically before non-simple requests to ask for permission. Your server must answer it with a 2xx status and the allow headers, without requiring authentication.
Is Access-Control-Allow-Origin: * insecure?
Not for public data that doesn’t depend on cookies or the user’s session. It’s a problem only for responses that contain private data, and browsers refuse * together with credentials anyway.
Is CORS the same as CSRF protection?
No. CORS controls who may read responses; the requests themselves are still sent and executed. Protect state-changing endpoints with authentication and CSRF tokens or SameSite cookies.
Conclusion
To fix a CORS error, read the full console message, check the preflight and the real request in the Network tab, and reproduce both with curl and an Origin header. Then fix it on the server: allow the exact origin (or * for public data), the methods and headers you use, credentials if you send cookies, and make sure only one layer sends the headers, including on error responses. During development, a dev-server proxy avoids CORS entirely.
How did it make you feel?
