How to Fix CORS Error in Laravel (and Stop the Browser Blocking Your API)

Your Laravel API works in Postman but the browser blocks every request with a CORS error. The fix is on the server, not in your JavaScript โ€” this guide shows how to read the console message, test the preflight with curl, and fix config/cors.php for good.

RH Raja HunainAuthor 30 Nov 2026 9 min read 27 views Updated 01 Dec 2026
On this page
Share

Explanation

Your API works perfectly in Postman. You switch to the browser, your React or Vue app fires the same request โ€” and the console turns red:

Access to fetch at 'http://localhost:8000/api/orders' from origin 'http://localhost:5173'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present
on the requested resource.

Nothing is wrong with your controller, your routes, or your JavaScript. The server answered the request happily; the browser simply refused to let your page read the answer. That is what a CORS error is: a browser rule about which websites may read responses from your server. Once you understand that, the fix becomes obvious โ€” you change what the server sends, not what the frontend asks for.

What CORS actually is (and why Postman never complains)

CORS stands for Cross-Origin Resource Sharing. An "origin" is the combination of scheme, host, and port, so http://localhost:5173 (your Vite dev server) and http://localhost:8000 (your Laravel API) are two different origins.

When JavaScript on one origin calls an API on another, the browser first asks the server a question: "Is this page allowed to read your response?" The server answers with Access-Control-* headers. If the answer is missing or wrong, the browser blocks your script from seeing the response โ€” even though the server already processed the request. Your POST may even have created the record before the browser threw the error, which is why CORS bugs sometimes leave duplicate data behind.

That is also why Postman and curl never show a CORS error. They are not browsers, so nobody enforces the rule on them. "It works in Postman" tells you the API itself is fine โ€” the problem is purely in the CORS headers.

For requests that send JSON, carry an Authorization header, or use PUT or DELETE, the browser goes one step further. Before the real request, it sends a silent OPTIONS request called a preflight, asking permission for the method and headers you want to use. Only if the preflight answer allows them does the browser send the real request. Most CORS failures are actually failed preflights.

Step 1: Read the console message properly

Every CORS console message names the exact rule that failed. Do not skip it โ€” it tells you which fix to apply:

  • "No 'Access-Control-Allow-Origin' header is present" โ€” the server sent no CORS header at all for this URL. Usually the route is outside the paths Laravel checks, or the request never reached Laravel.
  • "The 'Access-Control-Allow-Origin' header has a value 'https://example.com' that is not equal to the supplied origin" โ€” the server allows a different origin than your page's. An old value, a missing port, or a cached config is the usual cause.
  • "Response to preflight request doesn't pass access control check" โ€” the OPTIONS request failed. The real request never left the browser.
  • "The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true'" โ€” you are sending cookies or credentials, but the server did not allow them.

Next, open the Network tab and find the failing row. Is it the OPTIONS preflight or the real request? What status code did it get? That single observation narrows the problem to one of the fixes below.

Step 2: Test the preflight with curl

Send the same preflight the browser sends and read the headers that come back:

curl -i -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'

A healthy answer looks like this:

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: content-type, authorization

If curl gets no Access-Control-* headers back, no frontend change will help. Fix the server config first, then re-run this command โ€” when curl sees the headers, the browser will too.

Step 3: Publish config/cors.php and set your origin

Laravel ships with CORS handling built in. The HandleCors middleware sits in the global middleware stack and answers from the fruitcake/php-cors library. But in Laravel 11 and newer, a fresh application has no config/cors.php file โ€” the framework keeps its defaults internally. If you want to change anything, publish the file first:

php artisan config:publish cors

Open config/cors.php. The defaults look like this:

return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],
    'allowed_methods' => ['*'],
    'allowed_origins' => ['*'],
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*'],
    'exposed_headers' => [],
    'max_age' => 0,
    'supports_credentials' => false,
];

With these defaults, any API route under /api/* works from any origin. When it does not, one of the settings below is the reason. Put your real frontend origin in an environment variable instead of hardcoding it, so staging and production can differ:

// config/cors.php
'allowed_origins' => [env('FRONTEND_URL', 'http://localhost:5173')],
// .env
FRONTEND_URL=http://localhost:5173

Then clear the cached config so Laravel actually reads the change:

php artisan config:clear

A cached config is one of the most common CORS causes: you edit cors.php, nothing changes, because Laravel is still reading the cached copy. After any config change, run config:clear (or config:cache once you are done) before testing again.

Fix 1: Your route is not in paths

HandleCors only adds headers to URLs that match the paths setting. The default covers api/* and sanctum/csrf-cookie โ€” nothing else. If your frontend calls a route in routes/web.php, or a custom prefix such as /v1/* or /webhooks/*, it gets no CORS header at all, and the browser blocks it.

The cleanest fix is to move API routes under api/, where they belong. If you cannot, add the path pattern:

'paths' => ['api/*', 'sanctum/csrf-cookie', 'v1/*', 'webhooks/*'],

This is the fix when the console says "No 'Access-Control-Allow-Origin' header is present" and your API works fine in Postman.

Fix 2: The origin does not match exactly

Origins are compared exactly, character by character. http://localhost:5173 and http://localhost:5173/ are different strings, and so are http://localhost:5173 and http://localhost:3000. A trailing slash, a wrong port, or an old production domain copied into .env from staging will all produce the "header has a value that is not equal to the supplied origin" message.

Copy the origin straight from the console error message and paste it into your config โ€” do not type it from memory. If your frontend runs on many subdomains, use a pattern instead of listing each one:

'allowed_origins_patterns' => ['https://*.example.com'],

Fix 3: Credentials and the wildcard do not mix

If your frontend sends cookies โ€” for example a Sanctum SPA login with withCredentials: true โ€” the rules change. A wildcard origin (*) is not allowed together with credentials; the browser rejects the response outright. You must name the exact origin and turn credentials on:

'allowed_origins' => [env('FRONTEND_URL', 'http://localhost:5173')],
'supports_credentials' => true,

For a Sanctum SPA, also check SANCTUM_STATEFUL_DOMAINS in .env โ€” it must list your frontend domain, and SESSION_DOMAIN must match your cookie domain.

Fix 4: A 500 error is stripping your headers

Sometimes the preflight passes with a clean 204, but the real request still fails. Check the status code of the real request: if it is a 500, the CORS error is a symptom, not the disease. When your controller crashes, Laravel's exception handler builds the error response โ€” and that response often carries no CORS headers. The browser reports a CORS error, but the real bug is the exception in your code. Fix the 500 first (check storage/logs/laravel.log), and the CORS error disappears with it.

The same thing happens one layer up. If Nginx or Cloudflare intercepts the OPTIONS request before it reaches Laravel, the preflight never gets Laravel's headers at all. On the server, confirm that OPTIONS requests reach PHP and that no proxy strips the Access-Control-* headers on the way back.

What NOT to do

Three "fixes" you will find online make the error go away without fixing anything:

  • A proxy in your Vite or webpack config. Fine as a development workaround, but it hides the real config and teaches you nothing about production.
  • mode: 'no-cors' in fetch. This does not grant permission โ€” it makes the response opaque, meaning your JavaScript cannot read it at all. Your request "succeeds" and your app still has no data.

All three are fine for a five-minute local test. None of them is a fix โ€” the fix is always the server's CORS headers.

Frequently asked questions

Is CORS the same as CSRF? No. CORS controls which websites may read responses from your API. CSRF protection stops other websites from submitting forms as your logged-in user. Laravel's 419 Page Expired error is CSRF protection, not CORS โ€” the two errors look similar in the console but have different fixes.

Do I need a package to fix CORS in Laravel? No. Laravel 11 and 13 handle CORS out of the box with the built-in HandleCors middleware. Older tutorials install fruitcake/laravel-cors or barryvdh/laravel-cors โ€” those packages are obsolete now.

Why does the error appear only after I add an Authorization header? A plain GET with no custom headers is a "simple request" and skips the preflight. The moment you add Authorization or a JSON body, the browser sends a preflight first โ€” and a preflight that was never tested before is where the missing header shows up.

Conclusion

A CORS error in Laravel is the browser telling you that your server's answer is missing the permission headers your page needs. Start by reading the console message, reproduce the preflight with curl, then fix the one setting that matches: publish config/cors.php, add your route to paths, match the origin exactly, clear the cached config, and never combine a wildcard origin with credentials. The API you built with a REST API in Laravel 13 works โ€” and the JavaScript Fetch API calling it only needs the server to say yes in the right headers.

Was this helpful? 0 likes Login to like & bookmark
RH

Written by

Raja Hunain

Full Stack Developer at Raja Dev Solutions โ€” writes about real development work, the errors he faced and exactly how he fixed them, plus practical Laravel, PHP, JavaScript and MySQL tutorials.

More about the author

Comments (0)

No comments yet. Be the first to comment.

Leave a comment

Comments are moderated and appear after approval.

Keep reading

Related Errors & Articles

How to Fix "Target class [Controller] does not exist" Error in Laravel
Error & Fix Debugging & Troubleshooting 27 Nov 2026 ยท 3 min read

How to Fix "Target class [Controller] does not exist" Error in Laravel

You add a route, open the page, and Laravel crashes with "Target class [UserController] does not exist". This guide explains what the error really means, how to read the class name in the square brackets to find the cause in seconds, and the five fixes that cover every common case โ€” from the old string route syntax to a wrong namespace, a case-sensitive filename, and a stale route cache.

How to Fix 419 Page Expired Error in Laravel
Error & Fix Debugging & Troubleshooting 24 Nov 2026 ยท 8 min read

How to Fix 419 Page Expired Error in Laravel

The 419 Page Expired error in Laravel almost always means one thing: the CSRF token in your request did not match the one in your session. This guide shows how to find which of the seven usual causes is yours โ€” and fix it in minutes.