๐ง CORS: cheat sheet
๐จ The analogyA careful parent who won't pass a stranger's note the reply unless the office sends a permission slip.
๐ The regular explanationBrowsers stop pages reading responses from other origins (the same-origin policy). CORS is how a server opts in, with Access-Control-Allow-* headers and, for riskier requests, an OPTIONS preflight.
- A page from a stranger asks the browser to fetch your bank details. Your bank would recognise you, because the browser carries your cookies.
- The careful parent won't pass the answer to a stranger's note. It goes in the bin.
- A note from the bank's own family is fine. Same home, same rules.
- The shop's page wants reviews from a different office. The parent adds a label saying who's asking.
- The office sends the reviews with a permission slip, "shop.com may read this". The parent hands them over.
- The rule is for the parent, not the office. The office decides who gets a slip, and the parent checks it.
- This note wants to change things, not just read. The careful parent rings the office first, "would you accept this?"
- The office replies, "yes, from shop.com, for these kinds of changes, and you needn't ask again today".
- Only now does the parent send the real request, and the answer goes back to the note.
๐งฉ Key questions
- Why is the browser a careful parent?
- Because it carries your cookies, and a stranger's page could ask it to fetch your bank details. It will happily fetch things, but won't hand the answer to a page from somewhere else without permission.
- The office replies without a permission slip naming the page. What does the parent do?
- Keeps the answer from the page. No slip, no answer. The office may still have received the note.
- For notes that want to change things, what does the parent do first?
- Rings the office to ask whether it accepts this kind of request from this page. Only after a yes does the real request go.