The Permission Letter at the Gate

A simple request (for example, a plain GET) from a page to another origin still arrives. But the browser only gives the reply to the page when the server…

A page on myshop.com wants to show the shipping cost from courier.example, another house (another origin). The browser sends the request, the courier's server answers, but MyShop's page may not be allowed to read that reply.

The browser is the guard

When a page from one origin asks another origin for data with a simple request (for example, a plain GET), the browser still sends the request, and the server still answers. But before giving the reply to the page, the browser checks: did the server write a permission for the origin of the page that asked?

That permission is in the notes (headers) of the reply: Access-Control-Allow-Origin: https://myshop.com. If the page's origin is named there, the browser gives the reply to the page. If not, the browser blocks the reply, and the console (the place for error messages in DevTools) shows a CORS error. Other requests, for example ones that send JSON, are checked with the server first (a preflight), and are not sent at all if the server does not allow them.

The server grants, the browser guards

This permission rule is called CORS. The server decides who may read. The browser makes sure the rule is kept, so a dishonest site cannot secretly read another site's data in your name.

TL;DR: A simple request (for example, a plain GET) from a page to another origin still arrives. But the browser only gives the reply to the page when the server wrote a permission for that page's origin. This permission rule is called CORS.

⚡ The request still arrives

Guess first: a page on myshop.com asks courier.example, another origin, for the shipping cost with a plain GET. The courier's server writes no permission for myshop.com in its reply. Does the request reach the courier's server?

It does, and the courier's server answers. But the browser blocks the reply, so the page cannot read it, and the console (the place for error messages in DevTools) shows a CORS error.

Meaning: Before giving a reply from another origin to the page, the browser checks: did the server write a permission for the origin of the page that asked?

⚡ The server writes the permission

Write:

Access-Control-Allow-Origin: https://myshop.com

Meaning: This permission is in the notes (headers) of the server's reply. Here, pages from the origin https://myshop.com are allowed. If the origin of the page that asked is named there, the browser gives the reply to the page.

⚡ The server grants, the browser guards

Meaning: This permission rule is called CORS. The server decides who may read its reply, and the browser makes sure the rule is kept, so a dishonest site cannot secretly read another site's data in your name.

What you will learn