CORS (Cross-Origin Resource Sharing) is a browser-enforced security mechanism that uses HTTP headers to control whether JavaScript on one origin can access resources from a different origin.
Browsers block cross-origin requests by default; same origin means identical protocol, domain, and port
GET/HEAD/POST with safe headers go directly; PUT/DELETE/PATCH or custom headers trigger an automatic OPTIONS preflight request
Access-Control-Allow-Origin (who), Allow-Methods (how), Allow-Headers (what), Max-Age (cache duration), Allow-Credentials (cookies)
Missing Allow-Origin header, preflight failure, wildcard with credentials; fix is always server-side, not client-side
CORS errors are opaque to JS (generic TypeError); check browser DevTools console and Network tab for actual details
CORS extends the browser's Same-Origin Policy, which blocks scripts from making requests to a different origin (protocol + domain + port). Servers opt in to cross-origin access by sending specific HTTP headers. CORS is enforced only by browsers — tools like Postman and server-to-server requests are unaffected.
https://example.com and https://api.example.com are different origins (different subdomain). http://example.com and https://example.com are different origins (different protocol).text/plain, multipart/form-data, or application/x-www-form-urlencoded. These go directly to the server without a preflight.Authorization or Content-Type: application/json — triggers a preflight. The browser automatically sends an OPTIONS request first to check if the server allows the actual request.Access-Control-Allow-Origin: The primary header. Set to a specific origin (https://example.com) or * (wildcard). With credentials: 'include', wildcard * is not allowed — you must specify the exact origin.Access-Control-Allow-Methods: Lists permitted HTTP methods (e.g., GET, POST, PUT, DELETE).Access-Control-Allow-Headers: Lists permitted request headers (e.g., Authorization, Content-Type).Access-Control-Max-Age: How long (in seconds) the browser can cache the preflight result, avoiding repeated OPTIONS requests.Access-Control-Allow-Credentials: Set to true to allow cookies and auth headers in cross-origin requests.credentials: 'include' with Access-Control-Allow-Origin: * is invalid. Fix: set the header to the specific requesting origin.fetch() rejects with a generic TypeError. Check the browser DevTools console and Network tab for the actual error details.Key Interview Distinction: CORS is Server-Side
You cannot fix CORS errors from the client. Adding headers to the fetch request does not help — the server must send the Access-Control-Allow-* response headers. This is the most common misconception candidates demonstrate in interviews.
Fun Fact
Before CORS existed, developers used JSONP (JSON with Padding) — a clever hack that exploited the fact that script tags have no same-origin restriction. The server would wrap JSON data in a function call like callback({data}), and the browser would execute it. JSONP only supported GET requests and was a massive security risk, which is why CORS was standardized in 2014 to replace it.