How CORS Works — Preflight, Headers & Fixing CORS Errors
CORS (Cross-Origin Resource Sharing) is the browser mechanism that controls whether JavaScript on one origin may read a response from another. This guide explains the same-origin policy, preflight requests, the Access-Control-* headers, and how to fix the CORS errors that block your fetch calls.
Last updated:
Core Concepts
- The same-origin policy and why CORS exists
- By default, browsers block JavaScript from reading responses from a different origin (scheme + host + port) — the same-origin policy, which stops a malicious site from reading your bank's API using your cookies. CORS is the controlled exception: a server can send Access-Control-* headers that tell the browser 'it's OK for this specific origin to read my response'. CORS loosens the policy; it isn't a restriction the server imposes but a permission it grants.
- Simple requests vs preflight
- For 'simple' requests (GET/POST/HEAD with safe headers) the browser just sends the request and checks the response's Access-Control-Allow-Origin. For anything else — a custom header, Content-Type: application/json, or methods like PUT/DELETE — the browser first sends a preflight OPTIONS request asking 'am I allowed to do this?'. The server must answer with Access-Control-Allow-Methods/Headers before the real request is sent.
- The key headers
- Access-Control-Allow-Origin names which origin(s) may read the response (* for any, or a specific origin). Access-Control-Allow-Methods and -Allow-Headers answer the preflight. Access-Control-Allow-Credentials: true lets cookies/Authorization be sent cross-origin — but it cannot be combined with *, you must echo a specific origin. Access-Control-Max-Age caches the preflight so the browser doesn't re-ask every time.
- Why CORS is server-side, not a client fix
- A CORS error appears in the browser console, but the fix is almost always on the server — it must send the right Access-Control-* headers. You can't disable CORS from the client (that's the point: it protects users). Common workarounds while developing are a same-origin proxy or the server adding your origin to its allow-list. Browser extensions that 'disable CORS' only work locally and are not a deployment solution.
Frequently Asked Questions
How do I fix a CORS error?
Fix it on the server, not the client. The server serving the response must send Access-Control-Allow-Origin with your origin (or *), and for preflighted requests also Access-Control-Allow-Methods and Access-Control-Allow-Headers. If you send cookies, use a specific origin plus Access-Control-Allow-Credentials: true (not *). If you don't control the server, proxy the request through your own origin.
What is a CORS preflight request?
An automatic OPTIONS request the browser sends before certain cross-origin requests (custom headers, JSON content type, or non-simple methods) to check whether the actual request is allowed. The server answers with Access-Control-Allow-Methods/Headers; only then does the browser send the real request.
Is CORS a security feature on the server?
CORS is enforced by the browser to protect users, and configured by the server via headers. It doesn't protect your server from non-browser clients (curl, servers) — they ignore CORS entirely. Treat CORS as browser access control, and use real authN/authZ for actual server security.
Try these related tools
CSP Generator →
Build a Content-Security-Policy header by picking directives and sources. Generate a ready-to-paste CSP for script-src, style-src, img-src, connect-src and more — with sensible strict defaults. 100% local.
HTTP Status Codes →
Complete reference of HTTP status codes with meanings, use cases, and RFC citations. Search 60+ codes from 100 Continue to 511 Network Authentication Required.
cURL Converter →
Convert cURL commands to Python (requests), JavaScript (fetch), Node.js, Go (net/http), PHP, or Java (HttpClient). Parses -H, -d, --data-urlencode, -F, -u, --json and more. 100% local.
URL Parser →
Parse any URL into scheme, host, port, path, and query parameters — or build a URL from scratch with a form. Great for debugging redirect chains, tracking pixels, and OAuth flows. 100% local.