In this lesson you will
- Explain what an origin is and what exactly CORS blocks
- Recognise when a request needs a preflight and read the preflight answer
- Fix CORS errors on the server, including exposed headers and credentials
Origins, and what the browser actually blocks
An origin is the scheme, host and port together. https://needmytools.com and https://www.needmytools.com are different origins, and so are http://localhost:3000 and http://localhost:5173. By default, the same-origin policy lets a page read responses only from its own origin. Without it, any website you visit could quietly call your webmail or your bank in the background, using your cookies, and read the result.
CORS is how a server relaxes that rule on purpose. When JavaScript calls another origin, the browser adds an Origin header and, when the response arrives, looks for Access-Control-Allow-Origin. JSONPlaceholder echoed our origin back, so the browser handed the data to the page. example.com sent no such header, so the browser kept the response to itself. Notice that the request still reached example.com: CORS controls who may read, not who may send. That is why curl, Postman and other servers never see CORS errors; there is no browser in the middle to enforce it.
Console
- Console messages, errors, form data and clicked links appear here.
In Chrome this printed status 200, type cors for JSONPlaceholder, then TypeError: Failed to fetch for example.com, then status 0, type opaque for the no-cors try, and Page origin: null because examples run in a sandboxed frame. Firefox words the error "NetworkError when attempting to fetch resource" and Safari "Load failed". The real reason appears only in the console.
HTTP/2 200
content-type: application/json; charset=utf-8
access-control-allow-credentials: true
access-control-allow-origin: https://shop.example
vary: Origin, Accept-EncodingHTTP/2 200
date: Mon, 30 Nov 2026 10:31:59 GMT
content-type: text/html; charset=utf-8
server: cloudflare
last-modified: Thu, 26 Nov 2026 17:29:13 GMT
allow: GET, HEAD
accept-ranges: bytes
cf-cache-status: MISS
cf-ray: a6292f81fd45a577-ORD
alt-svc: h3=":443"; ma=86400Both servers answered 200. The difference is one header: JSONPlaceholder copies the caller’s origin into access-control-allow-origin and adds vary: Origin so caches keep a separate copy per origin. example.com sends no CORS header at all.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 2 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Why does my API work in Postman but not in the browser?
Postman is not a browser and does not apply the same-origin policy. The server answered in both cases, but without the right Access-Control-Allow-* headers the browser refuses to give the answer to your page.
Is CORS a security feature for my server?
No. It protects users in their browsers. Anyone can still call your API from a script or a server, so every endpoint needs its own authentication and rate limits whatever your CORS settings are.
Can I turn off CORS in my browser while developing?
You can with flags or extensions, but it hides the real problem until your users hit it. A better habit is a development proxy, such as the one Vite offers, or correct headers on your local API.
Does CORS apply to images, scripts and stylesheets?
A plain <img>, <script> or <link> tag can load from any origin. CORS starts to matter when JavaScript wants to read the bytes, for example drawing an image on a canvas and exporting it, or loading web fonts, which browsers fetch in CORS mode.
Why do I see two requests for one fetch in the Network tab?
The first is the OPTIONS preflight and the second is your real request. Use Access-Control-Max-Age on the server so the browser can reuse the preflight answer instead of asking every time.
Sources
Facts in this lesson were checked against these official pages on 30 November 2026.
- MDN: Cross-Origin Resource Sharing (CORS)
- MDN: Same-origin policy
- MDN: Access-Control-Expose-Headers
- WHATWG Fetch Standard: CORS protocol
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.