Fix "blocked by CORS policy: No 'Access-Control-Allow-Origin' header"
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
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.
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
- The server sends no CORS headers: no CORS middleware is configured, or it does not cover the route being called.
- The
OPTIONSpreflight fails: the route does not handleOPTIONS(404, 405), redirects (to HTTPS or a login page) or requires authentication. - Origin written incorrectly:
httpinstead ofhttps, a different port, a trailing slash (http://localhost:5173/) or an unlisted subdomain. - Cookies with a wildcard:
credentials: 'include'requires an exact origin andAccess-Control-Allow-Credentials: true; the*wildcard is then rejected. - 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
-
Identify the blocked request
In the Network tab, find the failing request. If there is an
OPTIONSline (preflight), check its status code and response headers. You can reproduce the preflight with curl (macOS, Linux, Git 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" -
Allow the origin on the server (Node.js / Express)
Install the
corspackage (npm install cors) and list the allowed origins. Used withapp.use, it also answersOPTIONSrequests automatically. Register it before your routes.import express from 'express'; import cors from 'cors'; const app = express(); app.use(cors({ origin: ['http://localhost:5173', 'https://app.example.com'], credentials: true, })); -
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 editconfig/cors.phpand runphp artisan config:clear.// 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, ]; -
Send cookies correctly
If you rely on session cookies, add
credentials: 'include'on the client andAccess-Control-Allow-Credentials: trueon the server (credentials: trueorsupports_credentials). The origin must then be exact, never*.const res = await fetch('https://api.example.com/me', { credentials: 'include', headers: { Accept: 'application/json' }, }); -
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.// vite.config.js import { defineConfig } from 'vite'; export default defineConfig({ server: { proxy: { '/api': { target: 'http://localhost:8000', changeOrigin: true, }, }, }, }); -
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
alwaystoadd_headerso the header is sent on error responses too.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
- Fix "Cannot read properties of undefined" in JavaScript
- Fix "Unexpected token '<', "<!DOCTYPE "... is not valid JSON"
- Fix "Error: Cannot find module" in Node.js
- Fix "npm ERR! ERESOLVE unable to resolve dependency tree"
- Fix "Hydration failed because the server rendered HTML didn’t match the client" (React, Next.js)
- Fix "Too many re-renders. React limits the number of renders" in React
- Fix "Error: listen EADDRINUSE: address already in use" in Node.js
- Fix "SyntaxError: Cannot use import statement outside a module"