Last reviewed: 22 Nov 2026
A new developer joined the team on our Campus School ERP, cloned the React frontend and the Laravel API, ran both, and got a blank dashboard. The Chrome console said: The 'Access-Control-Allow-Origin' header has a value 'https://erp.example.com' that is not equal to the supplied origin. Production was fine. He had already added Access-Control-Allow-Origin: * to an Axios header (which does nothing) and was about to install a browser extension that turns CORS off.
The cause took two minutes to find once we read the message properly. His .env was copied from staging, so Laravel only allowed the production origin, and the old value was also sitting in a cached config. CORS errors look scary, but the browser tells you exactly which rule failed. The fix is always on the server; React can only change what it sends.
All console messages and headers below were captured on my test box with a fresh Laravel 13.43 API, php artisan serve on port 8000, a page on http://localhost:5173 standing in for the Vite dev server, and Chromium 159.
Quick diagnosis
CORS is the browser asking the API: "may a page from this origin read your response?" The origin is scheme + host + port, so http://localhost:5173 and http://localhost:8000 are different origins. For anything beyond a simple GET (JSON body, Authorization header, PUT, DELETE) the browser first sends an OPTIONS preflight. If that answer is wrong, your real request never leaves the browser.
- Copy the full console message. The part after "blocked by CORS policy:" names the rule.
- Look at the Network tab. Is the failing row the
OPTIONSpreflight or the real request? What status did it get? - Reproduce with curl. If curl does not get the right header back, no frontend change will help.
curl -si -X OPTIONS http://localhost:8000/api/orders \
-H 'Origin: http://localhost:5173' \
-H 'Access-Control-Request-Method: POST' \
-H 'Access-Control-Request-Headers: content-type,authorization' | grep -i '^access-control'
Fixes by cause
1. "No 'Access-Control-Allow-Origin' header is present"
Access to fetch at 'http://localhost:8000/reports/daily' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.Laravel's built-in HandleCors middleware only adds headers to URLs that match paths in config/cors.php. The default is ['api/*', 'sanctum/csrf-cookie']. My request went to a route in routes/web.php, so it got no header at all:
$ curl -si http://localhost:8000/reports/daily -H 'Origin: http://localhost:5173' | grep -iE '^(HTTP|access-control)'
HTTP/1.1 200 OK
$ curl -si http://localhost:8000/api/orders -H 'Origin: http://localhost:5173' | grep -iE '^(HTTP|access-control)'
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *Move API routes under api/ or add the path pattern (for example 'reports/*', or 'login', 'logout' for Sanctum SPA auth). On Laravel 11 and later the file is not there until you run php artisan config:publish cors.
The same message also appears when PHP never answered: nginx returned a 502 because PHP-FPM crashed, or a 413 because an upload was bigger than client_max_body_size. Those error pages come from nginx, not Laravel, so they carry no CORS headers. A Laravel exception is different: on my box, a route that throws still returned 500 with Access-Control-Allow-Origin, so the browser showed the real 500. If the Network tab shows 502 or 413, fix that first.
2. "has a value '...' that is not equal to the supplied origin"
This was our new developer's problem. When allowed_origins contains exactly one origin, Laravel sends that value on every response, whatever the request's origin was:
$ curl -si http://localhost:8000/api/orders -H 'Origin: http://localhost:5173' | grep -i '^access-control'
Access-Control-Allow-Origin: https://erp.example.comAccess to fetch at 'http://localhost:8000/api/orders' from origin 'http://localhost:5173' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://erp.example.com' that is not equal to the supplied origin. Have the server send the header with a valid value.List every frontend origin exactly, with scheme and port and no trailing slash. I keep it in .env so each environment sets its own:
// config/cors.php
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_origins' => explode(',', env('CORS_ALLOWED_ORIGINS', 'http://localhost:5173')),
'supports_credentials' => true,
# .env (local)
CORS_ALLOWED_ORIGINS=http://localhost:5173,https://erp.example.comWith two or more origins, Laravel echoes the request's origin only if it is in the list, and adds Vary: Origin so caches keep the answers apart. A request from an unlisted origin (I tried http://localhost:3000) gets no Access-Control-Allow-Origin at all. For preview deployments with changing subdomains, use allowed_origins_patterns with a tight regex rather than *.
3. Wildcard with credentials
... has been blocked by CORS policy: 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'.You get this when React sends cookies (fetch(url, { credentials: 'include' }) or Axios withCredentials: true, which Sanctum SPA auth needs) while the API still has the default '*' and supports_credentials => false. Set supports_credentials to true and list exact origins. The fixed response then looks like this:
Access-Control-Allow-Origin: http://localhost:5173
Vary: Origin
Access-Control-Allow-Credentials: trueOne warning from testing: with supports_credentials => true and allowed_origins => ['*'], Laravel echoes any origin back with credentials allowed. That makes the error go away, but it lets every website make logged-in requests to your API. Do not ship it.
For Sanctum's cookie-based SPA auth you also need the frontend host in SANCTUM_STATEFUL_DOMAINS, a matching SESSION_DOMAIN, and a call to /sanctum/csrf-cookie before login. If CORS passes but login returns 419, that is CSRF, covered in Laravel 419 Page Expired; token vs cookie setup is in my Laravel Sanctum authentication guide.
4. The preflight fails
... has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'https://erp.example.com' that is not equal to the supplied origin."Response to preflight request" means the OPTIONS request failed, so the POST was never sent. Your controller and auth middleware never see it. A healthy Laravel preflight is a 204:
HTTP/1.0 204 No Content
Access-Control-Allow-Origin: http://localhost:5173
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: content-type,authorization
Access-Control-Max-Age: 0If yours is 401, 403, 404 or 405, something in front of HandleCors answered it: an nginx location that rejects OPTIONS, a gateway that requires auth on every method, or a custom middleware added before it. Let OPTIONS reach Laravel and let HandleCors answer. Once it works, a max_age such as 3600 saves a preflight on every call.
5. "contains multiple values"
... has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:5173, *', but only one is allowed.Two layers both added the header. I reproduced it with a small proxy that adds Access-Control-Allow-Origin: * in front of Laravel, which is what an nginx add_header line does:
access-control-allow-origin: http://localhost:5173
access-control-allow-origin: *Pick one place. With Laravel 11+ I keep CORS in config/cors.php and remove every add_header Access-Control-* from nginx, Apache .htaccess and any old fruitcake/laravel-cors or hand-written CORS middleware.
6. You changed the config and nothing happened
If production or a Docker image ran php artisan config:cache, Laravel ignores .env and config/*.php until the cache is cleared. I edited CORS_ALLOWED_ORIGINS on the test box and the preflight still returned the old origin until I ran:
$ php artisan config:clear
INFO Configuration cache cleared successfully.In deploy scripts, run php artisan config:cache again after the new .env is in place, and restart PHP-FPM or Octane workers.
For local development: skip CORS with the Vite proxy
If the React app and API will share a domain in production (for example erp.example.com and erp.example.com/api), the simplest dev setup makes the browser see one origin:
// vite.config.js
export default defineConfig({
server: { proxy: { '/api': 'http://localhost:8000', '/sanctum': 'http://localhost:8000' } },
});
// React: call a relative URL
const res = await fetch('/api/orders', { credentials: 'include' });When the frontend and API live on different domains in production, configure CORS properly as above and test it against staging, not only locally.
Verify the fix
- The curl preflight returns
204with your exact origin, andAccess-Control-Allow-Credentials: trueif you send cookies. - In the Network tab the
OPTIONSrow is 204 and the real request returns your data. On my box the fixed setup returned200for the GET with credentials and201 {"id":1043}for the POST. - The console is clean in a normal window with no CORS extension.
Common mistakes
- Setting
Access-Control-Allow-Originas a request header in Axios. It is a response header; sending it can even trigger a preflight. - Using
mode: 'no-cors'. The request "succeeds" but the response is opaque and React cannot read it. - Writing
http://localhost:5173/with a trailing slash, orlocalhostwhen the app runs on127.0.0.1. Origins must match exactly. - Fixing CORS in nginx and Laravel at the same time, which gives duplicate headers.
- Shipping
'*'with credentials, or a CORS extension as "the fix" for the team. - Reading the error as a JavaScript bug. If the response never arrives, later code fails with errors like Cannot read properties of undefined; fix the CORS message first.
Questions I get asked
Why does Postman or curl work when the browser fails?
CORS is a browser rule. Postman and curl do not enforce it, so they show the response even when the headers would block a page from reading it.
Is CORS a security feature for my API?
It protects users' browsers, not your server. Anyone can still call your API from a script, so you still need authentication and rate limiting.
Do I need the fruitcake/laravel-cors package?
Not on Laravel 9.2 and later. CORS handling is built in; remove the old package and its middleware to avoid duplicate headers.
Why does only the POST fail when GET works?
A plain GET is a "simple" request with no preflight. The POST with JSON triggers an OPTIONS preflight, so check that response.
About the author. Muhammad Wilayat Shah is a senior full-stack engineer and team lead based in Islamabad, with 13+ years shipping ERP, HMS, SaaS and property-portal products on Laravel, PHP, React, Next.js and PostgreSQL. React frontends talking to Laravel APIs are most of his day job, and this checklist is the one he gives new developers. More at wilayats.com/about.
Last reviewed: 22 Nov 2026




Comments
Join the discussion. Replies load on demand, up to 5 levels.
0 threads
No comments yet
Be the first to start the conversation.