Skip to main content

Browser · 6 common causes

Blocked by CORS policy

Updated

The browser blocked your JavaScript from reading a cross-origin response because the server never said your origin is allowed to.

The exact message

Access to fetch at 'https://api.example.com/data' from origin 'http://localhost:3000'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present
on the requested resource. If an opaque response serves your needs, set the request's
mode to 'no-cors' to fetch the resource with CORS disabled.

GET https://api.example.com/data net::ERR_FAILED
Uncaught (in promise) TypeError: Failed to fetch

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is Blocked by CORS policy.

How to fix it

Cause 1 of 6

The API does not send an Access-Control-Allow-Origin header.

This is fixed on the server, never in the browser. Send Access-Control-Allow-Origin: https://your-app.com from the API — in Express, app.use(cors({ origin: 'https://your-app.com' })). Avoid * on anything authenticated: it is incompatible with credentials and opens the endpoint to every site on the internet.

Cause 2 of 6

The preflight request is failing.

Any request with a custom header, or a content type other than the three simple ones, sends an OPTIONS preflight first. The server must answer that with a 2xx plus Access-Control-Allow-Methods and Access-Control-Allow-Headers covering what you send. Open the Network tab — the failing request is the OPTIONS, not the GET you wrote.

Cause 3 of 6

You send credentials but the server replies with a wildcard origin.

With credentials: 'include', the specification requires the server to echo one exact origin and to send Access-Control-Allow-Credentials: true. A * is rejected in that combination, by design.

Cause 4 of 6

The request actually failed and the error handler dropped the CORS headers.

A 500 that bypasses your CORS middleware surfaces in the browser as a CORS error, because the failed response carries no headers. Check the server logs and the real status code before changing any CORS config — the CORS message is a symptom here, not the cause.

Cause 5 of 6

You only need this to work in local development.

Use your dev server's proxy so the request is same-origin: server.proxy in Vite, rewrites in Next.js, proxy in Create React App. Do not disable web security in your browser or install an extension that strips CORS — that removes the protection for every site you visit, not just yours.

Cause 6 of 6

It is a third-party API with no CORS support at all.

Call it from your own backend and return the result to the browser from your own origin. Server-to-server requests are not subject to CORS, and it keeps the API key off the client, which a browser call cannot.

Stopping it coming back

Decide the allowed origins per environment in one place in the server config, and include a request from the real front-end origin in your API tests so a missing header fails in CI rather than in a browser.

Blocked by CORS policy — common questions

Why does the request work in Postman but not the browser?
CORS is enforced by browsers, not by servers or HTTP itself. Postman, curl and your backend are not bound by it, so a request succeeding there tells you the endpoint works but says nothing about whether it sends the headers a browser needs.
Does mode: 'no-cors' fix it?
No. It stops the error being reported but gives you an opaque response — status 0 and no readable body — so you cannot use the data. It is only useful for fire-and-forget requests where you never read the reply.
Why does the server see my request if the browser blocked it?
For a simple request, the browser sends it and blocks your code from reading the response. The side effects have already happened on the server. That is another reason preflighted requests exist for anything non-idempotent.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor