Skip to content

16 min read

What is CORS and how to fix a CORS error

In one sentence: CORS is how a server tells the browser which other websites can read its responses from their code. The error comes from the browser, not the server, and it's fixed on the server, with a header.

What you will learn

  • Tell whether two addresses have the same origin, and explain what the same-origin policy protects.
  • Read a CORS error in the console and know which header is missing and who has to add it.
  • Tell a simple request, which reaches the server even if it fails, from one that needs a preflight request first.
  • Recognize the fake fixes (turning off security, extensions, no-cors mode, an asterisk everywhere) and apply the right one.

Before you start, read: 05-cookies.md

Imagine Luis, from the shop, launches the new website. The page lives at https://shop.example, and the orders come from an APIAPI stands for Application Programming Interface.The way a program offers its services to other programs, that is, what you can ask it for and how it answers. The frontend talks to the backend through an API.Go to definition on another server, https://api.shop.example. Luis clicks “My orders” and nothing happens. In the browser console there’s a red line:

Access to fetch at 'https://api.shop.example/orders' from origin 'https://shop.example' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Luis tries the same address with curl and it answers perfectly. He pastes it in the browser’s address bar, and it works there too. So who’s blocking it? Not the server: the browser, which has received the response and won’t let the page read it.

Many of the fixes going around make it worse. Here you’ll see why it happens and how to fix it: on the server.

The analogy

Imagine an assistant who runs errands and takes requests from anyone in your house, including guests you barely know. A guest asks them to go to your bank and bring back your statement. The assistant goes, shows your ID card and the bank hands over the envelope.

But before handing it over, the assistant looks at the stamp on the envelope: “May be delivered to…” If whoever made the request isn’t on the stamp, the assistant keeps the envelope and tells the guest: “I can’t give you this.”

The assistant is your browser; the guests, the pages you open; the bank, a server; the ID card, your CookieA small piece of data, with a name and a value, that a server asks the browser to keep with the Set-Cookie header. The browser sends it back in the Cookie header of its next requests to that site, and that’s how the server recognizes the same visitor.Go to definition; and the stamp, a response header called Access-Control-Allow-Origin.

Where the analogy breaks down

  • The errand gets done anyway. If the request was “make a transfer,” the bank makes it: the assistant only refuses to hand over the answer. That’s why CORS doesn’t protect the server from requests it doesn’t want.
  • For some unusual requests, the assistant calls the bank first to ask whether it accepts them. If the bank says no, they don’t go. That’s the preflight request, which you’ll see shortly.
  • Someone who goes to the bank on their own, without the assistant, gets the envelope without anyone checking the stamp: the stamp only binds the assistant. That’s what curl does.
  • The assistant doesn’t always carry your ID card: it depends on how the bank gave it to you and on whether the guest explicitly asks for it.

To the browser, every website has an Origin (on the web)What identifies a website to the browser: the scheme (http or https), the server name and the port. https://shop.example and https://www.shop.example are different origins; the path doesn’t count.Go to definition: the scheme (http or https), the server’s name and the PortA number, from 0 to 65535, that says which program on a computer a connection is for. The computer has an address; each listening program has its port.Go to definition. If any of the three changes, it’s a different origin. Compared with https://shop.example/products:

Address Same origin? Why
https://shop.example/cart Yes Only the path changes, and the path doesn’t count
http://shop.example/products No Another scheme: http instead of https
https://www.shop.example/ No Another name: the www counts too
https://api.shop.example/orders No Another name, even if it’s the same company
https://shop.example:8443/ No Another port: with no number, https uses 443

You saw each scheme’s default port in lesson 3 of Phase 0, and ports in lesson 5.

Imagine you have your bank’s website open in one tab and, in another, some random page. That page can make your browser send requests to the bank: with a form, with an image that points to its address or with code.

And some of them carry your cookies, the ones from lesson 5. A form or an image carries them depending on how the bank marked them (SameSite). A request made from the page’s code (with fetch) only carries them to its own origin, unless the code explicitly asks for them with credentials: 'include'.

If that page could read the responses, it would read your balance with your session. To prevent that, there’s the Same-origin policyThe browser rule by which a page’s code can only read the responses from its own origin. It can send requests to other origins, but not read what they answer, unless the server allows it with CORS.Go to definition: a page’s code can send requests to other origins, but not read their responses.

Sometimes a server wants other origins to read its responses: the shop’s API, for its page, or a public service, for anyone. That’s what CORSCORS stands for Cross-Origin Resource Sharing.The way a server tells the browser which other origins may read its responses from a page’s code, with headers such as Access-Control-Allow-Origin. The browser enforces it, not the server.Go to definition is for. It works with two HeaderInformation a protocol puts in front of its data to do its job. In HTTP they are text lines of the form “Name: value”, such as Host or Content-Type; in TCP or IP they are numbers in fixed positions, such as ports or addresses.Go to definition:

  1. On every request to another origin, the browser adds Origin, with the page’s origin: Origin: https://example.com. The page’s code can’t change it.
  2. The server answers and, if it wants to give permission, adds Access-Control-Allow-Origin with that origin, or with *, which means “anyone.”

The browser compares the two. If they match, it hands the response to the page. If not, or if the header isn’t there, it hides it, writes the error in the console and the code gets TypeError: Failed to fetch. That’s what you’ll do in “Try it,” from example.com:

  1. Page (example.com) → Browserfetch('…/cors-off.json')
  2. Browser → backenddesdecero.comGET /practicas/http/cors-off.json
    Origin: https://example.com
  3. backenddesdecero.com → Browser200 OK
    (no Access-Control-Allow-Origin)
  4. BrowserNobody has given example.com permission:
    the response isn't handed over
  5. Browser → Page (example.com)TypeError: Failed to fetch

Look at the 200 OK: the request arrived, and the server answered. If it had been a form that creates an order, the order would be created. CORS decides whether the page reads the response, not whether the request arrives.

A page from another site doing things in your name by taking advantage of your cookies is an attack with a name of its own: CSRFCSRF stands for Cross-Site Request Forgery.An attack in which a page makes your browser send a request to a site where you’re logged in, with your cookies, to do something on your behalf, such as changing your email or buying something. CORS doesn’t prevent it, because many requests reach the server even if the page can’t read the response.Go to definition. You’ll see its defenses, like SameSite or a secret code in every form, in Phase 6.

Non-simple requests ask for permission first

Section titled “Non-simple requests ask for permission first”

Look at the diagram again: the request went out without asking. The browser only does that with simple requests, the ones a website could already send with a form before CORS existed:

  • the HTTP methodHTTP stands for HyperText Transfer Protocol.The word every HTTP request starts with, which says what the client wants to do with what it asks for. GET is reading, POST sending data, PUT replacing, PATCH changing a part and DELETE deleting.Go to definition is GET, HEAD or POST;
  • if it has content, it’s from a form or plain text, not JSONJSON stands for JavaScript Object Notation.A format for writing data as text, with braces, quotes and colons: {"product":"mug","quantity":2}. People can read it, and so can almost any programming language, and it’s the most common format for the data that travels between a frontend and a backend.Go to definition;
  • and it carries no custom headers, like Authorization.

The rest (a PUT, a DELETE, a POST with JSON, any request with Authorization) could surprise an old server that never got them from another website. So the browser asks first with a Preflight requestThe check the browser sends to another origin before a request that isn’t simple (for example, one that deletes data or carries custom headers), to ask the server whether it accepts it. It uses the OPTIONS method, and if the server doesn’t allow it, the real request is never sent.Go to definition: a request with the OPTIONS method that says what’s coming next. If the server doesn’t authorize it, the real request isn’t sent.

This is how it looks with the shop’s API, the one you have in your VM:

  1. Page (shop.example) → Browserfetch: POST /api/orders with JSON
  2. Browser → The shop's APIOPTIONS /api/orders
    Origin: https://shop.example
    Access-Control-Request-Method: POST
    Access-Control-Request-Headers: content-type
  3. The shop's API → Browser204 No Content
    Access-Control-Allow-Origin: https://shop.example
    Access-Control-Allow-Methods: GET, POST
    Access-Control-Allow-Headers: Content-Type
    Access-Control-Max-Age: 600
  4. Browser → The shop's APIPOST /api/orders
    Origin: https://shop.example
  5. The shop's API → Browser200 OK
    Access-Control-Allow-Origin: https://shop.example
  6. Browser → Page (shop.example)The response

Access-Control-Max-Age: 600 lets the browser remember the permission for 600 seconds, without asking again (each browser also sets its own limit).

If the preflight request fails, Chrome says so in the message. This is what it writes when you try a DELETE from example.com to this very site, which doesn’t give permission:

Access to fetch at 'https://backenddesdecero.com/' from origin 'https://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

“Response to preflight request doesn’t pass access control check”: the DELETE never went out.

With cookies, the permission names the origin

Section titled “With cookies, the permission names the origin”

If the code asks to send cookies to another origin (credentials: 'include'), the server has to do two more things, or the browser hides the response:

  • answer Access-Control-Allow-Credentials: true;
  • and put the exact origin in Access-Control-Allow-Origin, never *.

The reason: * is meant for public data, which doesn’t change depending on who asks for it. With cookies, the response carries one specific person’s data, and the server has to say who it trusts with it.

The server doesn’t block anything: it answers and adds, or doesn’t add, a few headers. It’s the browser that reads them and decides, because it’s the one running other people’s code with your cookies.

curl doesn’t run anyone’s pages, and it only sends the cookies you give it. That’s why you’ll never see a CORS error with curl, and an address working with curl says nothing about whether it will work from a page. The same goes between servers: when your backend calls another API, there’s no browser, and there’s no CORS.

So the fix is on the server: make it answer with the right headers for the origins it trusts.

The goal: find out who’s really blocking, the server or the browser, and then open the shop’s API to a new page. For practice, there are two files on this site: cors-on.json, which gives permission to any origin, and cors-off.json, which gives it to nobody.

Open https://example.com in Chrome and open DevToolsThe developer tools that come with browsers such as Chrome or Firefox, opened with F12. Their Network tab shows every request the page makes, with its response and headers; the Console, the page’s errors; and Application (Storage, in Firefox), what the site stores in your browser, such as cookies.Go to definition, as in the phase introduction. Go to the Console tab. Whatever you type there runs as if it were the page’s code, with the origin https://example.com.

The first time you paste something, Chrome may ask you to type allow pasting. It’s a protection against scams that ask you to paste code into the console: only do it with code you understand, like this.

1. A file that gives permission

Ventana de terminal
await fetch('https://backenddesdecero.com/practicas/http/cors-on.json').then(r => r.json())

What you will see:

{cors: 'on', message: 'Any origin can read this file from JavaScript: its response carries Access-Control-Allow-Origin: *'}

Word by word:

  • fetch('…') requests that address, just as the page’s code would.
  • .then(r => r.json()): when the response (r) arrives, read its content as JSON.
  • await waits for everything to finish, and the console shows the result.

The console shows the file’s content as an object, on one line (the triangle on the left expands it). The page has read a file from another origin, because its response carries Access-Control-Allow-Origin: *. If you get an error, check that you’re in the example.com tab.

2. A file that doesn't give permission

Ventana de terminal
await fetch('https://backenddesdecero.com/practicas/http/cors-off.json').then(r => r.json())

What you will see:

Access to fetch at 'https://backenddesdecero.com/practicas/http/cors-off.json' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The same line, with another file. There’s the error: access from https://example.com has been blocked because the resource doesn’t carry the Access-Control-Allow-Origin header. Two more red lines come out: the one for the failed request, with net::ERR_FAILED, and TypeError: Failed to fetch, which is what the code gets.

In Firefox the message is different, but it names the same header.

Now open the Network tab and repeat step 2. The request to cors-off.json is in the list: it left the browser. In the status column, Chrome writes CORS error: it’s not a code from the server, but the browser’s decision.

The lab shows what DevTools doesn’t let you see in full: every step between the page, the browser and the server, with their headers. Start with “The usual error”, what you just caused, and notice that the server receives the GET before the browser says no. Then try “A POST with JSON”, where the real request isn’t even sent, and “The preflight approved”, which is what the shop’s API does in the VM. Under “The request” and “The server” you can change every detail.

lab · CORS

The request
The server
Access-Control-Allow-Methods (in the preflight)
Access-Control-Allow-Headers (in the preflight)
  1. 1. The page → The browser: fetch(…), with GET https://api.shop.example/orders and credentials: 'same-origin'

The page’s code calls fetch: a GET to https://api.shop.example/orders.

The outcome

Did it reach the server?
Not known yet
With the cookie?
Not known yet
Can the page read it?
Not known yet

What if you ask the server directly, with no browser?

3. The same file, with curl

Ventana de terminal
curl https://backenddesdecero.com/practicas/http/cors-off.json

What you will see:

{
"cors": "off",
"message": "Only backenddesdecero.com can read this file from JavaScript: its response carries no Access-Control-Allow-Origin"
}

The whole file, with no errors. The server gives the same response to the browser and to curl; the difference is that curl doesn’t apply the same-origin policy.

4. Look for the permission in the headers

Ventana de terminal
curl -sI https://backenddesdecero.com/practicas/http/cors-on.json | grep -i access-control
curl -sI https://backenddesdecero.com/practicas/http/cors-off.json | grep -i access-control

What you will see:

access-control-allow-origin: *

-I asks for the headers only, -s removes curl’s progress messages, and grep -i keeps the lines with access-control, ignoring case (lesson 2 of Phase 1). Only one line comes out, the first file’s: that absence in the second one is the whole error from step 2. It’s in lowercase because curl speaks HTTP/2 with this site (lesson 8).

Now, from the server’s side. Log in to the VM with multipass shell course. We assume you installed the shop in the phase introduction: if curl localhost:8080 doesn’t answer Ana and Luis's shop…, go back there. Without a VM, read the outputs: they’re from an Ubuntu 24.04 with nginx 1.24.0.

5. The API, as its page sees it

Ventana de terminal
curl -i -H 'Origin: https://shop.example' localhost:8080/api/orders

What you will see:

HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Tue, 13 Oct 2026 08:00:09 GMT
Content-Type: application/json
Content-Length: 14
Connection: keep-alive
Access-Control-Allow-Origin: https://shop.example
Vary: Origin
{"orders":[]}

-H adds a header, here the Origin the browser would add on the shop’s page, and -i shows the response headers. Look at Access-Control-Allow-Origin: https://shop.example: the permission, with the exact origin. Vary: Origin warns CacheA saved copy of an answer, so it doesn’t have to be fetched again. It saves time, at the cost of the copy possibly being stale until it expires.Go to definition that the response may change depending on the Origin. Your date will be different.

6. An order from another origin

Ventana de terminal
curl -i -H 'Origin: https://other.example' -d 'product=1' localhost:8080/api/orders
sudo tail -n 1 /var/log/nginx/access.log

What you will see:

HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Tue, 13 Oct 2026 08:00:09 GMT
Content-Type: application/json
Content-Length: 14
Connection: keep-alive
Access-Control-Allow-Origin: https://shop.example
Vary: Origin
{"orders":[]}
::1 - - [13/Oct/2026:08:00:09 +0000] "POST /api/orders HTTP/1.1" 200 14 "-" "curl/8.5.0"

You pretend to be https://other.example. With -d, curl sends a POST with form data, like an HTML form: a simple request. nginx handles it and writes it in its log, the last line (::1 is localhost).

The permission is for shop.example: a browser on other.example would hide the response, but the order would already be in the log. That’s the gap CSRF takes advantage of.

7. The preflight request

Ventana de terminal
curl -i -X OPTIONS -H 'Origin: https://shop.example' -H 'Access-Control-Request-Method: POST' -H 'Access-Control-Request-Headers: Content-Type' localhost:8080/api/orders

What you will see:

HTTP/1.1 204 No Content
Server: nginx/1.24.0 (Ubuntu)
Date: Tue, 13 Oct 2026 08:00:09 GMT
Connection: keep-alive
Access-Control-Allow-Origin: https://shop.example
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: Content-Type
Access-Control-Max-Age: 600
Vary: Origin

It’s the question the browser would ask before a POST with JSON: “From https://shop.example, may I send a POST with the Content-Type header?” nginx answers with a 204 (no content) and four permissions: the origin, the methods, the headers and how long the answer is valid. They’re the lines in the configuration’s if ($request_method = OPTIONS) block.

Now, the fix. Imagine Luis also publishes the page at https://www.shop.example, and the error shows up from there: the www counts. Open the configuration with sudo nano /etc/nginx/sites-available/shop (the editor from lesson 2 of Phase 1). Find the two lines with Access-Control-Allow-Origin (Ctrl + W searches) and, in both, change https://shop.example to https://www.shop.example. Save and exit.

8. Check and reload

Ventana de terminal
sudo nginx -t && sudo systemctl reload nginx

What you will see:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

It’s step 3 of the introduction. If nginx -t gives an error with a line number, look at that line: most likely, a deleted quotation mark.

9. The new origin, with permission

Ventana de terminal
curl -i -H 'Origin: https://www.shop.example' localhost:8080/api/orders

What you will see:

HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Tue, 13 Oct 2026 08:00:10 GMT
Content-Type: application/json
Content-Length: 14
Connection: keep-alive
Access-Control-Allow-Origin: https://www.shop.example
Vary: Origin
{"orders":[]}

The permission now names the new origin: the www page could read the response. If the old one still comes out, wait a second and repeat it: reload takes a moment.

To leave it as it was, make the change the other way around in both lines and repeat step 8.

Challenge: Luis wants the API to serve both pages, with and without www. Is it enough to put both origins in Access-Control-Allow-Origin, separated by a comma?Show answer

No. The header accepts a single origin (or *): with two, the browser considers it invalid and hides the response. What you do is compare each request’s Origin with a list and return the one that matches, with Vary: Origin so caches don’t mix up the responses. You’ll do it in your server’s code in Phase 3.

  • Fonts from Google Fonts load on websites all over the world. Fonts are requested in CORS mode (the crossorigin from lesson 9 of Phase 0), and that’s why fonts.gstatic.com answers with access-control-allow-origin: *.
  • Images from other websites show up on any page: displaying them is allowed. Reading their content from code is not.

And if you code:

  • localhost:5173 and localhost:3000 are different origins, because the port changes. That’s why your frontend in development runs into CORS when it calls your backend. Vite’s server.proxy avoids it: the browser asks its own origin, and Vite forwards the request to the backend.
  • A fetch with Content-Type: application/json or with Authorization triggers a preflight request: in the Network tab you’ll see another row with the same address and the type preflight.
  • In axios, credentials: 'include' is called withCredentials: true.
  • “CORS protects my server.” It protects the person using the browser from other pages reading their data. A simple request arrives even if the response is hidden, and curl doesn’t even notice. What you don’t want is stopped on the server (Phase 6).
  • Adding Access-Control-Allow-Origin to the request, in the fetch. It’s a response header: on the request it does nothing, and since it’s a custom header, it triggers a preflight request.
  • mode: 'no-cors' in the fetch. The error goes away, but the response arrives opaque: no status, no headers and no content to read. It only silences the warning.
  • Turning off the browser’s security or installing an extension that “removes CORS.” Your users won’t have it, and while you do, any page you visit could read your data from other websites.
  • Putting * everywhere, or sending back any Origin as is. * is fine for public data without cookies. Sending back whatever Origin arrives, with Access-Control-Allow-Credentials: true, is the same as turning the protection off: always compare with a list.
  • A real error disguised as CORS. If your API fails with a 500 or a 404 without the CORS headers, the console only shows the CORS error. In nginx it happens with add_header without always, which doesn’t add the header to error responses. Check the Status codeA three-digit number at the start of every HTTP response that sums up what happened. 200 means “all good,” 404 “that doesn’t exist” and 400 “your request is malformed.”Go to definition with curl before touching anything.
  • An origin is scheme, name and port. If any of the three changes, it’s a different origin.
  • The same-origin policy lets a page send requests to other origins, but not read their responses.
  • CORS is the permission the server gives with Access-Control-Allow-Origin, and the browser checks it. A simple request arrives even if the response is hidden; the rest ask first with OPTIONS, and without permission they aren’t sent.
  • With cookies, the permission has to name the exact origin, never *, and carry Access-Control-Allow-Credentials: true.
  • The error comes from the browser, never from curl, and it’s fixed on the server.
The shop’s page is at https://shop.example and its API at https://shop.example:8443. Does the page need CORS to read the API?

A page from another website submits a form to the shop’s /api/orders, with a normal POST. The API doesn’t give that origin CORS permission. What happens to the order?

Your page makes a fetch with the DELETE method to an API on another origin. The API answers the OPTIONS with a 405 and no CORS headers. Does the DELETE reach the server?

Luis tries his API with curl and it answers fine, but the CORS error shows up from the page. What does he conclude?

The API answers Access-Control-Allow-Origin: *, and the page makes a fetch with credentials: 'include' so its cookie goes along. What happens?

To get rid of the error, someone adds mode: 'no-cors' to the fetch. The error goes away. Can they read the response now?

phase-2 · lesson 6/8