How to fix a CORS error ("blocked by CORS policy")

Tested: we reproduced this error and confirmed the fix on Chrome 153 with a small test API on Ubuntu 24.04, November 29, 2026.

The server didn’t say your page is allowed to read its response, so the browser blocked it. Fix it on the server by sending an Access-Control-Allow-Origin header that names your page’s origin:

Access-Control-Allow-Origin: https://app.example.com

For anything other than a simple GET (JSON bodies, custom headers, PUT or DELETE), the server also has to answer the browser’s preflight OPTIONS request with the methods and headers it allows. Your code wasn’t wrong; the browser was protecting the API.

CORS stands for Cross-Origin Resource Sharing. Browsers only let a page read responses from its own origin, meaning the same scheme, host and port (https://app.example.com and https://api.example.com are different origins, and so are localhost:3000 and localhost:8080). CORS headers are how a server opts in to sharing with other origins.

Find which message you have

We built a small API, called it from a page on another origin in Chrome 153 and changed one header at a time. Each setup gave a different message:

Chrome’s message ends with What the server is missing Fix
No 'Access-Control-Allow-Origin' header is present on the requested resource Any CORS header Send Access-Control-Allow-Origin
Response to preflight request doesn't pass access control check CORS headers on the OPTIONS reply Handle OPTIONS and add the headers there too
Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response Permission for the header you sent Add Access-Control-Allow-Headers: Content-Type

In the second setup the GET worked and only the POST with a JSON body failed. That’s because a JSON Content-Type makes the browser send a preflight first.

Fix it in Express

The cors package handles the headers and the preflight:

import express from 'express';
import cors from 'cors';

const app = express();
app.use(cors({ origin: 'https://app.example.com' }));

For several origins, pass an array. If the browser sends cookies or an Authorization header with credentials: 'include', add credentials: true and name the origin; * won’t work with credentials.

Fix it in other frameworks

Most server frameworks have a standard way to send these headers. We tested Express only.

Framework Usual approach
Flask The flask-cors package: CORS(app, origins=["https://app.example.com"])
Django The django-cors-headers package, with CORS_ALLOWED_ORIGINS in settings
Spring Boot (Java) @CrossOrigin(origins = "https://app.example.com") on a controller, or a global CorsRegistry
ASP.NET Core builder.Services.AddCors(...) and app.UseCors(...)

Fix it anywhere else

Whatever your server, the response needs:

Access-Control-Allow-Origin: https://app.example.com

and the OPTIONS response also needs:

Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization

That’s what fixed our last test case: once the API answered OPTIONS with these, both the GET and the JSON POST went through.

In development: proxy instead

If you’re calling someone else’s API that doesn’t send CORS headers, you can’t fix it from the browser. Call it from your own server instead (a small backend route that fetches the data and returns it), where CORS doesn’t apply. For local development, send the request through your dev server so the browser sees one origin. In Vite:

// vite.config.js
export default {
  server: { proxy: { '/api': 'http://localhost:8801' } },
};

Then call fetch('/api/data') instead of the full URL. A proxy is a development fix; production still needs the server headers, or the page and API on the same origin.

Turning CORS off in Chrome

You’ll see advice to start Chrome with --disable-web-security and a separate --user-data-dir. That turns off the same-origin protection for every site in that browser window, and it only hides the error on your machine; your users still get it. Use it, if at all, in a throwaway profile for a quick test, and fix the server for real. We didn’t test this flag.

Confirm it worked

Open DevTools, go to the Network tab and click the request. The response headers should include Access-Control-Allow-Origin with your origin, and for a preflighted request you’ll see an OPTIONS row with status 204 or 200 before it. The console error should be gone. MDN’s CORS guide explains the rules in full.

FAQ

Can I fix CORS on the frontend?

Not for real. CORS is the server telling the browser who may read its responses. You can avoid it in development with a proxy, or by serving the page and the API from the same origin, but the lasting fix is a header on the server.

Why does the request work in Postman or curl?

CORS is enforced by browsers only. Postman and curl don't check Access-Control-Allow-Origin, so the same request succeeds there.

Is localhost the same origin as 127.0.0.1?

No. The browser compares scheme, host and port as text, so a page on http://127.0.0.1:8765 calling http://localhost:8801 is cross-origin. In our test that difference alone was enough to trigger the error.

Should I set Access-Control-Allow-Origin to *?

Only for public APIs that don't use cookies or auth headers. * can't be used together with credentials, and it lets any site read the response.