Resources / Guides
CORS errors explained: why the browser blocks your API call (and how to fix it)
"No Access-Control-Allow-Origin header" and preflight failures, explained — what CORS is, why the browser blocks requests, and the right fixes for your API.
You call your API from a web page and the console says:
Access to fetch at 'https://api.example.com/orders' from origin 'https://app.example.com'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present.
The request may even have reached your server. The browser just refuses to hand the response to your JavaScript. Here's why, and how to fix it properly.
What CORS is
Browsers apply the same-origin policy: a page from https://app.example.com can't read responses from another origin (a different scheme, host or port) unless that server says it's allowed. CORS (Cross-Origin Resource Sharing) is how the server says so — with response headers.
CORS is enforced by browsers only. curl, Postman, your backend and mobile apps never see CORS errors. That's why "it works in Postman" tells you nothing about CORS.
Simple requests vs preflight
- Simple requests —
GET,HEADorPOSTwith only basic headers and a form-style content type. The browser sends the request and checksAccess-Control-Allow-Originon the response. - Everything else —
PUT,DELETE,PATCH,Content-Type: application/json, anAuthorizationheader… The browser first sends anOPTIONSpreflight request asking permission. Only if the preflight answer allows the method and headers does it send the real request.
A preflight response looks like this:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: content-type, authorization
Access-Control-Max-Age: 600
The common errors and their fixes
| Error | Cause | Fix |
|---|---|---|
| No 'Access-Control-Allow-Origin' header | The response (or an error response) lacks the header | Send it on every response, including 4xx and 5xx |
| Response to preflight request doesn't pass | OPTIONS returns 404, 401 or a redirect | Answer OPTIONS with 2xx before auth and routing |
| Method not allowed by Access-Control-Allow-Methods | The preflight didn't list the method | List it in Access-Control-Allow-Methods |
| Request header not allowed | e.g. authorization or x-api-key not listed | Add it to Access-Control-Allow-Headers |
| Wildcard '*' with credentials | credentials: 'include' plus Allow-Origin: * | Echo the exact origin and send Access-Control-Allow-Credentials: true |
Fixes that don't work
- `mode: 'no-cors'` — the request goes out, but you get an opaque response you can't read. It hides the error; it doesn't fix it.
- Browser extensions that disable CORS — they only change your browser, not your users'.
- Public "CORS proxies" — someone else's server sees all your traffic and API keys.
The right fix
- If you control the API, add the headers there — on every response and on
OPTIONS. Use*for public, keyless APIs; list exact origins when you use cookies. - If you don't control the API (a third-party service that doesn't allow browsers), call it from your own endpoint instead. That also keeps its API key out of the browser — see hide API keys with a proxy endpoint.
CORS on Wisp
Wisp endpoints answer preflight requests and send Access-Control-Allow-Origin: * on their responses, so a browser app can call them without any setup. When you need a third-party API that blocks browsers, describe a proxy — "an endpoint that calls the weather API with our key" — and call that from the page instead.
Try it: curl to fetch / Python converter
Build it with Wisp — describe an API, get it live.