What is CORS?
Browsers keep sites apart with the same-origin policy: code from one origin (scheme, host and port) cannot read responses from another. CORS is the way a server opts in. It adds headers such as Access-Control-Allow-Origin that tell the browser which other sites may read its responses.
It matters every time a web app calls an API on another domain. For risky requests the browser first sends an OPTIONS preflight to ask permission. When the server does not answer with the right headers, you get the famous blocked by CORS policy error. The fix always lives on the server.
Think of it like this. Imagine a school where a teacher only hands a report card to a parent whose name is on the pickup list. Anyone can walk in and ask, but the teacher (the browser) only hands it over if the list from the office (the server's CORS headers) names that parent. For bigger requests, the teacher phones the office first to check (the preflight).
Step by step
Normal
3 stepsA weather widget on app.example.com reads data from api.example.net, and the API says that is fine.
Normal
3 stepsA weather widget on app.example.com reads data from api.example.net, and the API says that is fine.
- 1
The page loads
Code from app.example.com runs
The page's origin is its scheme, host and port: https, app.example.com, 443. Anything else, even another subdomain, is a different origin.
- 2
Simple request
GET with an Origin header
The code calls fetch on api.example.net. A plain GET with no special headers is a simple request, so the browser sends it at once and adds Origin.
- 3
Permission in the reply
Access-Control-Allow-Origin
The API answers with Access-Control-Allow-Origin naming the page's origin. The browser compares it and lets the page's code read the response.
Preflight allowed
4 stepsSaving a note sends a PUT with JSON and a login token, so the browser asks permission first.
Preflight allowed
4 stepsSaving a note sends a PUT with JSON and a login token, so the browser asks permission first.
- 1
Not a simple request
PUT, JSON and Authorization
Requests that could change data in ways old sites never expected need a check first. PUT, a JSON body and an Authorization header all trigger it.
- 2
Preflight
OPTIONS with Access-Control-Request-*
The browser sends an OPTIONS request describing what the real request will look like. No body, no cookies, no token.
- 3
Server agrees
Allow-Methods and Allow-Headers
The API lists the origin, methods and headers it accepts, and how long the browser may remember the answer with Access-Control-Max-Age.
- 4
Real request
The PUT goes out
Only now does the browser send the PUT. The response still needs Access-Control-Allow-Origin before the page can read it.
Preflight blocked
3 stepsThe API was never told to trust app.example.com, so the browser stops the request before it is sent.
Preflight blocked
3 stepsThe API was never told to trust app.example.com, so the browser stops the request before it is sent.
- 1
Preflight
Same OPTIONS as before
The page tries the same PUT. The browser sends the preflight first.
- 2
No permission
The reply has no Allow-Origin
The API answers the OPTIONS, but without any Access-Control-Allow-Origin header. Often the server simply was not set up for this site.
- 3
Blocked in the browser
The PUT is never sent
The browser refuses to send the real request and the fetch fails with a CORS error in the console. Nothing was changed on the server. The fix is on the server, not in the page.
Cookies and *
3 stepsThe request was sent and answered, but the browser hides the answer because a wildcard cannot share private data.
Cookies and *
3 stepsThe request was sent and answered, but the browser hides the answer because a wildcard cannot share private data.
- 1
Request with cookies
credentials: include
The page asks for Arjun's profile and wants his api.example.net cookies sent too. It is a simple GET, so there is no preflight.
- 2
Server answers
200 OK with Allow-Origin: *
The server returns the profile with a wildcard. The request really did reach the server and run there.
- 3
Browser hides it
* is not allowed with cookies
For requests with cookies the browser needs the exact origin and Access-Control-Allow-Credentials: true. A wildcard is refused, so the page cannot read the reply. CORS guards reading, not sending.
Myths and mix-ups
- CORS protects your server from other sites; it protects users' data in the browser, and servers still need their own access checks.
- A CORS error means the request never reached the server; simple requests are sent, only the response is hidden.
- You can fix CORS from the page's JavaScript; only the server's response headers can grant access.
- Setting Access-Control-Allow-Origin to * works for everything; it does not work for requests with cookies.
- curl and server-to-server calls hit CORS too; only browsers enforce it.
Try it yourself
Run these on your own computer to see CORS at work.
Fake a preflight
curl -i -X OPTIONS https://api.example.net/v1/notes/42 -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: PUT' -H 'Access-Control-Request-Headers: authorization, content-type'Use your own API's address. Look for access-control-allow-* headers in the reply.
Check a normal response
curl -si https://api.example.net/v1/weather -H 'Origin: https://app.example.com' | grep -i access-controlShows whether the server allows that origin.
Watch it in the browser
F12, Network tab, filter by Fetch/XHRPreflights appear as OPTIONS rows (in Chromium, tick the Preflight option in the filter). Errors show in the Console tab.
CORS at a glance
- Layer
- Application (7), HTTP headers
- Enforced by
- The browser, not the server
- Key header
- Access-Control-Allow-Origin
- Preflight
- OPTIONS request for non-simple requests
- Origin is
- Scheme + host + port
- Defined in
- WHATWG Fetch Standard
- Port
- Whatever the API uses, usually 443
Defined inWHATWG Fetch StandardRFC 6454RFC 9110
Header fields
What CORS puts in its messages, with example values from the stories above.
HTTP (decrypted)
- Path
/- Page origin
https://app.example.comapi.example.net is a different host, so any request there is cross-origin.- Method
OPTIONSThe preflight. The page did not write it; the browser sends it on its own.- Origin
https://app.example.comAdded by the browser, not the page. It says which site's code is asking, and the page cannot fake it.- Sec-Fetch-Mode
corsTells the server this request comes from page code that wants to read the answer.- Access-Control-Allow-Origin
https://app.example.comThe permission slip. It must match the page's origin exactly, or be * for public data.- Vary
OriginTells caches the answer depends on who asked, so one site's permission is not served to another.- Access-Control-Request-Method
PUT- Access-Control-Request-Headers
authorization, content-typeAuthorization and a JSON content type are not on the safe list, which is why a preflight is needed.- Access-Control-Allow-Methods
GET, PUT, DELETE
Browser console
- Result
TypeError: Failed to fetchPage code only gets a generic error. The detail is in the console, so attackers learn nothing from it.- Real request sent
No- Request reached server
YesCORS does not stop requests from being sent. Servers still need their own checks, such as CSRF protection.
Test yourself
Who enforces CORS?
Questions people ask
What is CORS in simple words?
A set of HTTP headers a server sends to tell browsers which other websites may read its responses. Without them, browsers keep the data from other sites' code.
How do I fix the blocked by CORS policy error?
Change the server, not the page. Make it return Access-Control-Allow-Origin with your site's origin, answer OPTIONS preflights, and list any methods and headers you use.
What is a CORS preflight request?
An automatic OPTIONS request the browser sends before a non-simple request, asking whether that origin may use that method and those headers.
Why does my request work in curl or Postman but not in the browser?
Only browsers enforce CORS. Other tools send the request and show the reply whatever the headers say.
Is CORS a security feature?
It relaxes the browser's same-origin policy in a controlled way. It protects users' data in the browser, but it is not a replacement for logins, tokens and CSRF protection on the server.