First Principles
Frontend

Cross Origin Resource Sharing (CORS), What is it?

A clear explanation of CORS and the same-origin policy — how browsers use HTTP headers to safely allow cross-domain resource sharing.

By Fadjar Rafi. Published 16 Jun 2024. 3 min read.

Contents
  1. The same-origin policy
  2. What is CORS
  3. Why CORS matters
CORS error in the browser console

If you've built a web app, you've probably run into this error. It was a nightmare for me too when I first hit it while building an API for a blog. To see why it happens, we need to understand what CORS is and how it works.

The same-origin policy#

By default, a browser only lets a page request resources from its own origin — the same domain, protocol, and port. This is the same-origin policy (SOP), and it limits how a script from one origin can reach resources on another.

Say you have a web app at https://www.domain-a.com that needs data from an API at https://api.domain-b.com. Because the origins differ, this is a cross-origin request — and the SOP blocks it by default.

A cross-origin request blocked by the same-origin policy

So what do you do when your app genuinely needs to talk to another origin? That's where CORS comes in.

What is CORS#

Cross-origin resource sharing is how a server tells the browser which origins are allowed to access its resources. It works through HTTP headers added to the response — the browser reads them and decides whether to hand the data to your script.

There are several CORS headers, but the main ones are:

  • Access-Control-Allow-Origin
  • Access-Control-Allow-Methods
  • Access-Control-Allow-Headers

For a simple request like GET, POST, or HEAD, the browser sends it straight to the server and attaches an Origin header saying where it came from.

A CORS request and response exchanging Origin and Access-Control-Allow-Origin headers

Request from the browser:

GET /data HTTP/1.1
Host: api.domain-b.com
Origin: https://www.domain-a.com

Response from the server:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://www.domain-a.com
Content-Type: application/json

{ "message": "Hello from domain-b!" }

If the response echoes back an Access-Control-Allow-Origin that matches your origin, the browser lets the data through. Otherwise it blocks it — that's the error at the top of this post.

Requests that can change data — DELETE, PUT, PATCH, or anything with custom headers — are treated more carefully. Before sending the real request, the browser fires a preflight: an OPTIONS request that asks the server whether the actual request is allowed.

Preflight OPTIONS request from the browser:

OPTIONS /data HTTP/1.1
Host: api.domain-b.com
Origin: https://www.domain-a.com
Access-Control-Request-Method: DELETE
Access-Control-Request-Headers: Content-Type

Preflight OPTIONS response from the server:

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://www.domain-a.com
Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type

Once the preflight passes, the browser sends the real request and gets its response back.

DELETE request from the browser:

DELETE /data/123 HTTP/1.1
Host: api.domain-b.com
Origin: https://www.domain-a.com
Content-Type: application/json

Response from the server:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://www.domain-a.com
Content-Type: application/json

{ "message": "Resource deleted successfully!" }

Why CORS matters#

CORS puts the server in charge of who can read its resources. Without it, any site could quietly make requests to another domain on a user's behalf — the basis of attacks like cross-site request forgery (CSRF). With it, apps can still share data across origins, but only when the server explicitly opts in.