Skip to content
|By Muhammad Wilayat Shah|36 views • 32 unique IPs

CORS Error in React and Laravel: How to Fix It

A new developer on our school ERP got a blank React dashboard and a CORS error while production worked. Here is how I read the Chrome message, reproduce the preflight with curl and fix Laravel's CORS config.

ShareLinkedInX
Four steps to fix a CORS error between React and Laravel: read the Chrome console message, reproduce the preflight with curl -X OPTIONS and an Origin header, fix paths, allowed origins and credentials in config/cors.php, and run php artisan config:clear
Four steps to fix a CORS error between React and Laravel: read the Chrome console message, reproduce the preflight with curl -X OPTIONS and an Origin header, fix paths, allowed origins and credentials in config/cors.php, and run php artisan config:clear

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.

  1. Copy the full console message. The part after "blocked by CORS policy:" names the rule.
  2. Look at the Network tab. Is the failing row the OPTIONS preflight or the real request? What status did it get?
  3. Reproduce with curl. If curl does not get the right header back, no frontend change will help.
Code
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'
Animated checklist mapping six Chrome CORS console messages to their Laravel cause and fix: no Access-Control-Allow-Origin header means the route is not in the cors paths, a value not equal to the supplied origin means allowed_origins lacks your origin, wildcard with credentials include means you need the exact origin plus supports_credentials, a failing preflight means OPTIONS is blocked before HandleCors, multiple values means nginx and Laravel both add the header, and unchanged behaviour after editing means the config cache needs clearing
The six messages I see most on React + Laravel projects, each with the setting to change.

Fixes by cause

1. "No 'Access-Control-Allow-Origin' header is present"

Code
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:

Code
$ 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:

Code
$ curl -si http://localhost:8000/api/orders -H 'Origin: http://localhost:5173' | grep -i '^access-control'
Access-Control-Allow-Origin: https://erp.example.com
Code
Access 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:

Code
// 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.com

With 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

Code
... 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:

Code
Access-Control-Allow-Origin: http://localhost:5173
Vary: Origin
Access-Control-Allow-Credentials: true

One 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

Code
... 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:

Code
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: 0

If 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.

Animated terminal: a curl OPTIONS preflight to a Laravel 13 API with Origin http://localhost:5173 returns Access-Control-Allow-Origin https://erp.example.com, then after setting CORS_ALLOWED_ORIGINS in .env and running php artisan config:clear the same preflight returns Access-Control-Allow-Origin http://localhost:5173 with credentials, methods and headers allowed
The exact fix for our new developer: the right origin list, plus clearing a cached config.

5. "contains multiple values"

Code
... 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:

Code
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:

Code
$ 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:

Code
// 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 204 with your exact origin, and Access-Control-Allow-Credentials: true if you send cookies.
  • In the Network tab the OPTIONS row is 204 and the real request returns your data. On my box the fixed setup returned 200 for the GET with credentials and 201 {"id":1043} for the POST.
  • The console is clean in a normal window with no CORS extension.

Common mistakes

  • Setting Access-Control-Allow-Origin as 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, or localhost when the app runs on 127.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

ShareLinkedInX
Back to blog

Keep reading

Fix Nginx 413 Request Entity Too Large Error

11/25/2026

Fix Nginx 413 Request Entity Too Large Error

Nginx 413 Request Entity Too Large means a body size limit was hit. Find which layer sent it, then fix client_max_body_size, PHP upload limits, Express and Kubernetes ingress.

Newsletter

Occasional notes on Laravel, React, and shipping production systems — no spam.

Comments

Join the discussion. Replies load on demand, up to 5 levels.

0 threads

Loading editor…
Reviewed before publishing

No comments yet

Be the first to start the conversation.