Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Browser blocks your Laravel API with a CORS error

ErrorAccess to fetch at 'https://api.example.com/api/orders' from origin 'https://app.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Short answer

Laravel answers CORS preflights itself through its HandleCors middleware, driven by config/cors.php. The browser still blocks you when the route path is not listed in paths, when credentials are on but supports_credentials is false, or when the session cookie domain does not cover the front end's subdomain. Publish the file, edit it, and test with curl.

  • Applies to Laravel 10, Laravel 11, Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the preflight yourself against the exact URL the page calls. If the response has no Access-Control headers, the path is outside the CORS configuration; if it has them, the problem is credentials or cookies.

    curl -i -X OPTIONS https://api.example.com/api/orders \
      -H "Origin: https://app.example.com" \
      -H "Access-Control-Request-Method: POST"
  2. 2Match what you see, then jump to its fix

    1. 1No Access-Control-* headers on the OPTIONS response for a URL that does not start with api/The URL path is not covered by the paths optionFix
    2. 2The browser says credentials mode is include; the preflight response lacks Access-Control-Allow-CredentialsThe front end sends cookies but supports_credentials is falseFix
    3. 3CORS headers are correct, yet login works but the next request is unauthenticated (401 or 419)The session cookie is not valid for the SPA's subdomainFix

What does the error look like?

  • Access to fetch at 'https://api.example.com/api/orders' from origin 'https://app.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  • Response to preflight request doesn't pass access control check

Cause 1 of 3

The URL path is not covered by the paths option

1Check

The stock cors config only applies to api/* and sanctum/csrf-cookie. Routes in routes/web.php, a custom prefix, or an API prefix you changed in bootstrap/app.php do not match. In Laravel 11 and newer the config file is not in a fresh project; the framework uses built-in defaults.

php artisan route:list --path=orders
ls config/cors.php

2Fix

Publish the file, then add every path pattern your front end calls.

php artisan config:publish cors

// config/cors.php
'paths' => ['api/*', 'sanctum/csrf-cookie', 'webhooks/*'],
'allowed_origins' => ['https://app.example.com'],

3Verify

Repeat the preflight; Access-Control-Allow-Origin must now appear.

curl -si -X OPTIONS https://api.example.com/api/orders \
  -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: POST" | grep -i access-control

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The front end sends cookies but supports_credentials is false

1Check

In the stock file supports_credentials is false and allowed_origins is a wildcard. With credentials the browser accepts only an exact origin plus the credentials header; a wildcard is rejected.

grep -n "supports_credentials\|allowed_origins" config/cors.php

2Fix

Turn credentials on and list exact origins, and enable credentials on the HTTP client too.

// config/cors.php
'allowed_origins' => ['https://app.example.com'],
'supports_credentials' => true,

// resources/js/bootstrap.js (axios)
axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;

3Verify

The response carries Access-Control-Allow-Credentials: true and the exact origin.

curl -si https://api.example.com/sanctum/csrf-cookie \
  -H "Origin: https://app.example.com" | grep -i access-control

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

The session cookie is not valid for the SPA's subdomain

1Check

Sanctum's SPA mode needs the API and the SPA on the same top-level domain. The cookie domain in config/session.php decides which subdomains receive the cookie.

grep -n "'domain'" config/session.php

2Fix

Prefix the session domain with a dot so every subdomain of the root domain is covered, and list the SPA's host as stateful.

// config/session.php
'domain' => '.example.com',

// .env
SANCTUM_STATEFUL_DOMAINS=app.example.com

3Verify

Log in from the SPA and call an authenticated route; the browser should send the session cookie.

php artisan config:clear

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Keep the allowed origins in your .env file and read them in config/cors.php, one list per environment.
  • Never leave allowed_origins as a wildcard on an API that also uses cookies.
  • Test the preflight with curl after each deploy, not only in the browser.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.