Skip to content

Fix this error

CORS: No 'Access-Control-Allow-Origin' header is present

Dasun Kalupahana 289 views

The error

Error

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

Why it happens

The browser blocks a request from one origin (domain, port or protocol) to another unless the server allows it with CORS headers. In Laravel, the path is not in config/cors.php, the origin is not allowed, or the request fails before CORS headers are added (for example a 500 error).

How to fix it

  1. Publish the config if you don't have it: php artisan config:publish cors.
  2. Add the API paths and your front-end origin.
  3. Sending cookies (Sanctum SPA)? Set supports_credentials to true and use exact origins, not *.
  4. Clear the config cache and check the response in the browser's Network tab: a server error also shows up as a CORS error.
// config/cors.php
return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],
    'allowed_methods' => ['*'],
    'allowed_origins' => ['https://app.example.com'],
    'allowed_headers' => ['*'],
    'supports_credentials' => true,
];
Still stuck? Send us the full error and what you tried. We add the answers here.

Learn more on the blog

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.