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
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.
| Header | Purpose |
|---|---|
| Access-Control-Allow-Origin | Which origin may read the response (or * for any, without credentials) |
| Access-Control-Allow-Methods | Allowed methods for preflighted requests |
| Access-Control-Allow-Headers | Allowed request headers, e.g. content-type, authorization, traceparent |
| Access-Control-Allow-Credentials | true if cookies or auth headers are sent |
| Access-Control-Max-Age | How long the browser may cache the preflight result |
import cors from 'cors';
app.use(cors({
origin: ['https://app.example.com'],
credentials: true,
allowedHeaders: ['content-type', 'authorization', 'traceparent'],
}));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 sendVary: 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
traceparentheader 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.
Check it now, free
- HTTP headers checker: See the raw response headers of any URL.
Related reading
- Cloudflare error 1016 "Origin DNS error": how to fix it: Fix Cloudflare error 1016 Origin DNS error: CNAME records pointing to names that do not resolve, deleted origins and load balancer pools, with checks.
- Cloudflare error 1020: Access denied: Fix Cloudflare error 1020 Access denied: find the firewall rule that blocked a visitor using the Ray ID, tune WAF rules and allow your monitors and APIs.
- 403 Forbidden: what it means and how to fix it: Fix 403 Forbidden errors: file permissions, missing index files, WAF and bot rules, IP allowlists, expired signed URLs and missing API scopes, step by step.
- Cloudflare error 523: origin is unreachable: Fix Cloudflare error 523 Origin is Unreachable: wrong DNS IPs, routing problems at the host and firewalls, with tests that confirm where traffic stops.
- Mixed content errors: how to find and fix them: Fix mixed content warnings on HTTPS pages: find http:// images, scripts and API calls, update CMS URLs and add a Content-Security-Policy upgrade directive.
- ERR_BLOCKED_BY_CLIENT: how to fix it: Fix net::ERR_BLOCKED_BY_CLIENT: ad blockers and privacy extensions blocking scripts, images and API calls, and how developers avoid false blocks.
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.