Skip to content

CORS Explained: Origins, Preflight Requests, Exposed Headers and How to Fix CORS Errors

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Sooner or later every web developer sees a red console message that starts with "has been blocked by CORS policy". The same URL works in curl and in Postman, so the error feels random. It is not. Browsers protect users by refusing to let one website read answers from another unless that other server says it is allowed. CORS, Cross-Origin Resource Sharing, is the set of response headers a server uses to say so. This lesson shows the exact headers with real recorded traffic, lets you trigger a blocked request and a working one from your browser, explains the extra OPTIONS request called a preflight, and ends with the fixes that work and the ones that only hide the problem.

Quick answer

CORS is a browser rule. A page may send a request to another origin (a different scheme, host or port), but its JavaScript may read the answer only if the response carries Access-Control-Allow-Origin with that page’s origin or *. Requests with custom headers, JSON bodies or methods such as PUT and DELETE first trigger a preflight OPTIONS request, and the server must allow the method and headers. You fix CORS on the server or with your own proxy, never in the browser code.

35 min read and practice · Content checked on 30 November 2026

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.

Try it: A blocked request and an allowed one
Code
ResultUpdates as you type
Console
  1. 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.

Example: One API that allows other origins and one site that does not
Real output: curl -i https://jsonplaceholder.typicode.com/todos/1 -H "Origin: https://shop.example" (status, type and CORS headers)
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-Encoding
Real output: curl -I https://example.com/ -H "Origin: https://shop.example" (all headers)
HTTP/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=86400

Both 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.