What is CORS?

In short
CORS, short for Cross-Origin Resource Sharing, is a browser standard that lets a server decide which other websites may read its responses. The server does this by sending special HTTP headers, such as Access-Control-Allow-Origin. It relaxes the browser's default rule that blocks one site from reading data from another.
What CORS is for
Browsers follow a safety rule called the same-origin policy. Two addresses share an origin only when their scheme, host and port all match. Without extra permission, a page from one origin cannot read the responses that come from a different origin. You can read more in our guide to the same-origin policy.
CORS is a way for a server to open a controlled exception to that rule. It works through extra HTTP headers. The server sends them, and the browser reads them and decides whether the page may see the response.
CORS is not a lock on the server. The server still receives every request that reaches it. CORS only tells the browser whether a web page from another origin may read the answer.
How it works
When a page asks for something from another origin, the browser adds an Origin header to the request. This header tells the server which page started the call. The server replies with an Access-Control-Allow-Origin header. If that value matches the requesting origin, or is the wildcard star, the browser lets the page read the response. If it does not match, the browser blocks the page.
Some requests can change data on the server, so the browser checks them first. For those, it sends a preflight request. A preflight uses the OPTIONS method to ask the server what it accepts. Only after the server approves does the browser send the real request.
Servers can also say whether credentials may be included. Credentials are things such as cookies and HTTP authentication details. If you want to learn more about cookies, see our page on how cookies work.
When a CORS check fails, the browser blocks the response and the JavaScript receives only a generic error. For security reasons the code cannot see exactly what went wrong. To find the reason, open the browser console, which usually shows a clear message about the missing or wrong header.
Simple requests
Some requests skip the preflight step. These are often called simple requests. They must use GET, HEAD or POST. They may set only a short list of headers that browsers consider safe, such as Accept, Accept-Language, Content-Language and Content-Type. The Content-Type must be one of three values: application/x-www-form-urlencoded, multipart/form-data or text/plain.
The reason for this rule is history. A plain HTML form has always been able to send these kinds of requests to any site. So letting scripts send the same kind of request adds little new risk. The server must still opt in by sending Access-Control-Allow-Origin, or the script cannot read the reply.
A simple call looks like this:
const response = await fetch('https://api.example.com/items'); const data = await response.json();
If the API at api.example.com sends back a matching Access-Control-Allow-Origin header, the data reaches the page. If not, the call fails in the browser even though the server responded.
Preflighted requests
Anything outside the simple rules gets a preflight. A common example is a POST with a Content-Type of text/xml or a custom header such as X-PINGOTHER. The browser first sends an OPTIONS request. That request includes Access-Control-Request-Method, which names the method the real call will use, and Access-Control-Request-Headers, which lists the headers it will send.
The server answers the preflight with Access-Control-Allow-Methods and Access-Control-Allow-Headers. These list what the server accepts. It can also send Access-Control-Max-Age, which tells the browser how many seconds it may remember the answer. The documentation example uses 86400, which is one day. Once the approval arrives, the browser sends the actual request. That real request does not carry the Access-Control-Request headers, because those are only for the preflight.
Common mistakes
The first mistake is using the wildcard star for everything. A star lets any origin read the response. That may be fine for public data, but it is risky for private data. The second mistake is combining the star with credentials. When a request includes cookies or authentication, the server must name one specific origin instead of the star.
The third mistake is forgetting the preflight. If a server does not answer OPTIONS requests correctly, the browser never sends the real request. Developers often see a failure on the first call and assume the main endpoint is broken, when the problem is the OPTIONS reply.
The fourth mistake is thinking CORS protects the server from all attackers. CORS only affects what browsers allow web pages to read. Tools such as curl or a backend program ignore CORS completely. Your server still needs proper checks for who can call it and what they can do. For input checks, see our page on input validation.
When you need it
You need CORS when a page loaded from one origin calls an API on another origin. The documentation also lists other cases where browsers apply CORS, including images drawn to a canvas where the code wants to read pixel data, web fonts loaded through the font-face rule, and modules loaded by script tags.
When you set it up, allow only the origins you trust. Send the headers only on the responses that need them. Test a real cross-origin call in the browser, and read the console when something fails. Keep the list of allowed methods and headers as short as your application allows.
FAQ
Is CORS a security feature for my server?
Not by itself. CORS controls which web pages in a browser may read a response from your server. It does not stop tools like curl or other backend programs from sending requests, so you still need authentication and input checks.
Why does my request fail even though the server returns 200 OK?
The browser checks the CORS headers before it hands the response to your code. If Access-Control-Allow-Origin is missing or does not match your page's origin, the browser blocks the reply. Check the browser console for the exact message.
Why is there an extra OPTIONS request in my network log?
That is a preflight request. The browser sends it before calls that may change data on the server, such as some POST requests with custom headers. The server must answer it with the allowed methods and headers before the real request is sent.
Can I use the star wildcard for every API?
You can, but it is only sensible for public data. Once a request includes credentials such as cookies, the server must name a specific origin instead of the star.
Sources
This page is based on the official documentation below. Read it for full detail.
CORS — official documentation ↗