JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsjavascriptCORS
Prev
javascript
advanced
11 min read

CORS

browser
cors
fetch
http
preflight
same-origin-policy
security

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.

Key Points

1Same-Origin Policy

Browsers block cross-origin requests by default; same origin means identical protocol, domain, and port

2Simple vs Preflight

GET/HEAD/POST with safe headers go directly; PUT/DELETE/PATCH or custom headers trigger an automatic OPTIONS preflight request

3CORS Response Headers

Access-Control-Allow-Origin (who), Allow-Methods (how), Allow-Headers (what), Max-Age (cache duration), Allow-Credentials (cookies)

4Common Errors

Missing Allow-Origin header, preflight failure, wildcard with credentials; fix is always server-side, not client-side

5Debugging

CORS errors are opaque to JS (generic TypeError); check browser DevTools console and Network tab for actual details

What You'll Learn

  • Understand what CORS is and why the browser enforces it
  • Know the difference between simple requests and preflight requests
  • Know how to diagnose and resolve common CORS errors

Deep Dive

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.

Same-Origin Policy

  • Two URLs have the same origin only if they share the same protocol, domain, and port. 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).
  • Without CORS headers, the browser blocks the response from reaching JavaScript — the request may still reach the server, but the browser refuses to expose the response.

Simple Requests vs Preflight

  • A "simple" request uses GET, HEAD, or POST with only CORS-safelisted headers and Content-Type of text/plain, multipart/form-data, or application/x-www-form-urlencoded. These go directly to the server without a preflight.
  • Any other request — PUT, DELETE, PATCH, or requests with custom headers like 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.
  • The motivation: HTML forms could always make simple cross-origin POST requests, so servers were already expected to handle those. Preflight protects against newer request types that older servers may not expect.

Key CORS Headers

  • 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.

Common CORS Errors and Fixes

  • "No Access-Control-Allow-Origin header present" — the server isn't sending CORS headers. Fix: add the header server-side.
  • Preflight failure — the server doesn't handle OPTIONS requests or doesn't allow the method/headers used. Fix: configure the server to respond to OPTIONS with the correct Allow headers.
  • Wildcard with credentials — using credentials: 'include' with Access-Control-Allow-Origin: * is invalid. Fix: set the header to the specific requesting origin.
  • For third-party APIs you don't control, use a reverse proxy or backend relay — CORS headers must come from the server, not the client.

Debugging CORS

  • CORS errors are intentionally opaque to JavaScript — fetch() rejects with a generic TypeError. Check the browser DevTools console and Network tab for the actual error details.
  • The request often succeeds at the network level (the server processes it), but the browser blocks JavaScript from reading the response.

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.

Practice What You Learned

What is CORS, when do CORS errors occur, and how can they be resolved?
senior
cors
CORS (Cross-Origin Resource Sharing) is a browser security mechanism that blocks cross-origin requests unless the server explicitly allows them via HTTP headers like Access-Control-Allow-Origin.
Previous
IIFE (Immediately Invoked Function Expression)
Prev