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

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

ErrorCauseFix
No 'Access-Control-Allow-Origin' headerThe response (or an error response) lacks the headerSend it on every response, including 4xx and 5xx
Response to preflight request doesn't passOPTIONS returns 404, 401 or a redirectAnswer OPTIONS with 2xx before auth and routing
Method not allowed by Access-Control-Allow-MethodsThe preflight didn't list the methodList it in Access-Control-Allow-Methods
Request header not allowede.g. authorization or x-api-key not listedAdd it to Access-Control-Allow-Headers
Wildcard '*' with credentialscredentials: 'include' plus Allow-Origin: *Echo the exact origin and send Access-Control-Allow-Credentials: true

Fixes that don't work

The right fix

  1. 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.
  2. 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.

Related