Skip to content

Error explained

CORS error: blocked by CORS policy

The browser blocked your page from reading a response from another origin, because the server did not say that origin is allowed. The fix is on the server, not in the front-end.

3 min read10 sectionsUpdated

On this page

Key points

  • CORS is enforced by browsers, not servers; curl will work fine.
  • The API must send Access-Control-Allow-Origin for your page’s origin.
  • Preflight OPTIONS requests must succeed for non-simple requests.
  • Credentials cannot be combined with "*".

How CORS works

An origin is scheme + host + port: https://app.example.com and https://api.example.com are different origins. When a page calls another origin, the browser sends the request (or first a preflight OPTIONS request for anything beyond simple GET/POST with simple headers) and checks the response headers. If they do not allow the page's origin, the browser blocks the response from your code.

The headers that matter

HeaderPurpose
Access-Control-Allow-OriginWhich origin may read the response (or * for any, without credentials)
Access-Control-Allow-MethodsAllowed methods for preflighted requests
Access-Control-Allow-HeadersAllowed request headers, e.g. content-type, authorization, traceparent
Access-Control-Allow-Credentialstrue if cookies or auth headers are sent
Access-Control-Max-AgeHow long the browser may cache the preflight result

Server examples

javascript
import cors from 'cors';
app.use(cors({
  origin: ['https://app.example.com'],
  credentials: true,
  allowedHeaders: ['content-type', 'authorization', 'traceparent'],
}));
nginx.confplaintext
location /api/ {
    if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin "https://app.example.com" always;
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
        add_header Access-Control-Allow-Headers "content-type, authorization" always;
        add_header Access-Control-Max-Age 600 always;
        return 204;
    }
    add_header Access-Control-Allow-Origin "https://app.example.com" always;
    proxy_pass http://api;
}

Common mistakes

  • Using * with credentials: browsers reject it; echo the specific allowed origin instead (and send Vary: Origin).
  • The preflight hits authentication middleware and gets 401; let OPTIONS through.
  • Error responses (4xx/5xx) missing CORS headers, so the browser hides the real error behind a CORS message.
  • Front-end tracing adds a traceparent header that the API does not allow; add it to the allowed headers.

Catching it in production

A blocked request makes fetch reject with a TypeError ("Failed to fetch"). If your code does not handle it, real user monitoring records it as a JavaScript error, often with no matching server error, because the server answered and the browser hid the response.

VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.

Frequently asked questions

Can I fix CORS in the front-end?

Not properly. A development proxy can avoid it locally, but in production the API must send the right headers.

Why does it work in Postman but not the browser?

Postman is not a browser and does not enforce CORS. The browser does.

Check it now, free

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.