Skip to content
FR
  1. LuzineDebug
  2. JavaScript & Node.js
  3. Access to fetch at 'https://api.example.com/user…
JavaScript & Node.js Intermediate ≈ 20 min to fix

Fix "blocked by CORS policy: No 'Access-Control-Allow-Origin' header"

Error message
Access to fetch at 'https://api.example.com/users' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  • Access to XMLHttpRequest at 'https://api.example.com/login' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
  • Access to fetch at 'https://api.example.com/me' from origin 'http://localhost:5173' 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'.
  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/users. (Reason: CORS header 'Access-Control-Allow-Origin' missing). Status code: 200.
  • Origin http://localhost:5173 is not allowed by Access-Control-Allow-Origin. Status code: 200
In short

The browser blocks the response because the API server did not allow your page’s origin. The fix is on the server: send the Access-Control-Allow-Origin header with your exact origin and answer the OPTIONS preflight request correctly.

Be the first to confirm this fix

What this error means

An origin is the protocol + domain + port triple: http://localhost:5173 and http://localhost:8000 are two different origins. For security, the browser forbids a page from reading a response from another origin unless that origin explicitly allows it through CORS headers. For "non-simple" requests (JSON, Authorization header, PUT or DELETE method) it first sends an OPTIONS preflight request. If the preflight fails or the header is missing, the response is blocked. The request often did reach the server: it is the browser that refuses to expose the response. Postman and curl are not affected.

Common causes

  1. The server sends no CORS headers: no CORS middleware is configured, or it does not cover the route being called.
  2. The OPTIONS preflight fails: the route does not handle OPTIONS (404, 405), redirects (to HTTPS or a login page) or requires authentication.
  3. Origin written incorrectly: http instead of https, a different port, a trailing slash (http://localhost:5173/) or an unlisted subdomain.
  4. Cookies with a wildcard: credentials: 'include' requires an exact origin and Access-Control-Allow-Credentials: true; the * wildcard is then rejected.
  5. Server error without headers: a 500 error or a page returned by a proxy (Nginx, Cloudflare) skips the CORS middleware and hides the real error.

Step-by-step fix

  1. Identify the blocked request

    In the Network tab, find the failing request. If there is an OPTIONS line (preflight), check its status code and response headers. You can reproduce the preflight with curl (macOS, Linux, Git Bash).

    bash
    curl -i -X OPTIONS https://api.example.com/users \
      -H "Origin: http://localhost:5173" \
      -H "Access-Control-Request-Method: POST" \
      -H "Access-Control-Request-Headers: content-type, authorization"
  2. Allow the origin on the server (Node.js / Express)

    Install the cors package (npm install cors) and list the allowed origins. Used with app.use, it also answers OPTIONS requests automatically. Register it before your routes.

    javascript
    import express from 'express';
    import cors from 'cors';
    
    const app = express();
    
    app.use(cors({
      origin: ['http://localhost:5173', 'https://app.example.com'],
      credentials: true,
    }));
  3. Allow the origin on the server (Laravel)

    The CORS middleware is enabled by default. Publish its config with php artisan config:publish cors (Laravel 11+), then edit config/cors.php and run php artisan config:clear.

    php
    // config/cors.php
    return [
        'paths' => ['api/*', 'sanctum/csrf-cookie'],
        'allowed_methods' => ['*'],
        'allowed_origins' => ['http://localhost:5173', 'https://app.example.com'],
        'allowed_origins_patterns' => [],
        'allowed_headers' => ['*'],
        'exposed_headers' => [],
        'max_age' => 0,
        'supports_credentials' => true,
    ];
  4. Send cookies correctly

    If you rely on session cookies, add credentials: 'include' on the client and Access-Control-Allow-Credentials: true on the server (credentials: true or supports_credentials). The origin must then be exact, never *.

    javascript
    const res = await fetch('https://api.example.com/me', {
      credentials: 'include',
      headers: { Accept: 'application/json' },
    });
  5. In development: use a proxy

    With Vite, a proxy routes calls through the same server as the page, so there is no cross-origin call anymore. Then call fetch('/api/users') with a relative path. This does not replace CORS configuration in production if the API is on another domain.

    javascript
    // vite.config.js
    import { defineConfig } from 'vite';
    
    export default defineConfig({
      server: {
        proxy: {
          '/api': {
            target: 'http://localhost:8000',
            changeOrigin: true,
          },
        },
      },
    });
  6. Look for hidden errors

    If the CORS error only shows up on some requests, check the server logs: a 500 or 502 error returned without CORS headers looks like a CORS problem. With Nginx, add always to add_header so the header is sent on error responses too.

    nginx
    add_header Access-Control-Allow-Origin "https://app.example.com" always;
    add_header Vary Origin always;

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Keep the list of allowed origins in an environment variable per environment (dev, staging, prod) instead of hard-coding it.
  • Only use Access-Control-Allow-Origin: * for a public API without cookies or private data: otherwise any website can read the responses.
  • Avoid mode: 'no-cors' and extensions that disable CORS: the first makes the response unreadable, the second hides a problem your users will still hit.

Frequently asked questions

Why does my request work in Postman but not in the browser?

CORS is enforced only by browsers. Postman, curl or a Node.js server do not check it, hence the difference.

Can a CORS error be fixed on the front-end only?

No, the permission must come from the server that responds. On the front-end you can only go through a proxy you control (Vite proxy in dev, or your own back-end).

Does mode: 'no-cors' fix the error?

It makes the message go away, but the response becomes "opaque": you can read neither its status nor its body. It is not a solution for calling an API.

Useful tools for this error

More JavaScript & Node.js errors

All JavaScript & Node.js errors →