ResourceInformation

Library › Developer Pain

🧱 Developer Pain

Why CORS Errors Happen (and How to Fix Them)

Library · 26 November 2026 · 6 min read

You call your API from your website and the browser console turns red: "blocked by CORS policy". The same request works perfectly in Postman. It's one of the most-searched errors in web development, and it feels like the browser is broken. It isn't. Once you see what the browser is protecting, the fix becomes obvious, and it's almost always on the server.

⏱ Developer Pain series: the integrations and errors that waste the most developer hours, explained with moving pictures.

1. What "origin" means

An origin is three parts of a web address: the scheme (https), the host (shop.com) and the port (443, hidden by default). The path after it doesn't matter. If any one of the three is different, it's a different origin.

Page on https://shop.com calls…Same origin?Why
https://shop.com/api/cart✅ Yesonly the path is different
https://api.shop.com❌ Nodifferent host (a subdomain counts)
http://shop.com❌ Nodifferent scheme
https://shop.com:8080❌ Nodifferent port
http://localhost:3000 → http://localhost:8080❌ Nodifferent port: the classic local-development case

Try it: same origin checker

Paste the address of your page and the address your code is calling. Nothing is sent anywhere: the check runs in your browser.

SchemeHostPort
Press "Check"

2. Why the browser blocks it

Imagine you're logged in to your bank in one tab. In another tab you open a shady website. Its hidden JavaScript tries to fetch bank.com/balance. If browsers allowed that, any website you visit could quietly read your email, your bank and your company's internal tools, all using your logged-in session.

So browsers follow the same-origin policy: a page's JavaScript may freely read responses from its own origin, but not from other origins, unless that other server says it's okay. That "unless the server says okay" part is CORS: Cross-Origin Resource Sharing.

WITHOUT PERMISSIONthe server answers, but the browser hides the reply🌐 myshop.comyour page's codefetch(api…)🗄️api.myshop.combrowser checkGET /productsserver says 200 OK ✓200 + data200 + data✖ Blocked by CORS policy: No 'Access-Control-Allow-Origin' header
The request usually reaches the server and the server answers. It's the browser that refuses to hand the reply to your code, because the server didn't give permission.

That's also why Postman, curl and your backend never see this error. They aren't browsers holding a user's cookies and logins, so the rule doesn't apply to them.

3. The fix: the server gives permission

Every cross-origin request from a browser carries an Origin header saying where it came from. If the server's reply includes a matching Access-Control-Allow-Origin header, the browser lets your code read the reply.

WITH PERMISSIONthe server's reply includes Access-Control-Allow-Origin🌐 myshop.comyour page's codefetch(api…)🗄️api.myshop.combrowser checkOrigin: myshop.comserver says 200 OK ✓Allow-Origin: myshop.comdata✓ products shown✓ Origin is allowed: the browser hands the reply to your code
Same request, but this time the server's reply names your site. The browser's check turns green and your code gets the data.
Request (sent by the browser)        Response (sent by your server)
GET /products                        HTTP/1.1 200 OK
Origin: https://myshop.com           Access-Control-Allow-Origin: https://myshop.com
                                     Vary: Origin

This is the key idea: CORS is fixed on the server you're calling, not in your front-end code. No fetch option or front-end library can give itself permission. If you don't control that server (a third-party API without CORS support), call it from your own backend instead and have your page talk to your backend.

4. The preflight: "May I?"

Simple GET and basic form POST requests go straight out. But for anything that could change data in a way old websites never could, such as PUT, DELETE, a Content-Type: application/json body or custom headers like Authorization, the browser first sends a quick preflight request using the OPTIONS method:

🌐 Browser🗄️ API server① OPTIONS /cartOrigin: myshop.com · Method: PUT · Headers: Content-Type② 204 No ContentAllow-Origin: myshop.com · Allow-Methods: PUT · Allow-Headers: Content-Type③ PUT /cart { JSON }the real request, sent only after a yes④ 200 OK + dataAllow-Origin: myshop.com“May I?”“Yes.”
If the server doesn't answer the OPTIONS request correctly (or answers it with an error, a redirect or a login page), the real request is never sent.

A lot of "my CORS headers are set but it still fails" problems are here: the headers are added to the normal route, but the OPTIONS request hits an authentication check, a 404 or a redirect before it gets them. Make sure OPTIONS requests get a fast 204 with the Access-Control-Allow-Methods and Access-Control-Allow-Headers you need. Access-Control-Max-Age lets the browser remember the answer for a while, so it doesn't ask every time.

5. Fixes that work

Pick the one for your server. In every case, list your exact front-end origins.

Node.js (Express) with the cors package:

const cors = require("cors");
app.use(cors({ origin: ["https://myshop.com"], credentials: true }));

Django with django-cors-headers:

INSTALLED_APPS += ["corsheaders"]
MIDDLEWARE.insert(0, "corsheaders.middleware.CorsMiddleware")
CORS_ALLOWED_ORIGINS = ["https://myshop.com"]

Nginx in front of any backend:

location /api/ {
  add_header Access-Control-Allow-Origin  "https://myshop.com" always;
  add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
  add_header Access-Control-Allow-Headers "Content-Type, Authorization" always;
  if ($request_method = OPTIONS) { return 204; }
  proxy_pass http://backend;
}

Local development: let your dev server forward API calls, so the browser only ever talks to one origin. In Vite (vite.config.js):

export default { server: { proxy: { "/api": "http://localhost:8080" } } }

Now your page calls /api/products on its own origin, and the dev server passes it to port 8080 behind the scenes. React, Angular and Vue tooling all have the same feature.

Reading the error message

The console message tells you exactly which rule failed:

The message says…What it meansFix
No 'Access-Control-Allow-Origin' header is presentThe server didn't give permission at allAdd the header for your origin on the server
…must not be the wildcard '*' when the request's credentials mode is 'include'You send cookies or a login, but the server allows *Name the exact origin and add Access-Control-Allow-Credentials: true
Response to preflight request doesn't pass access control checkThe OPTIONS request failed (404, 401, redirect, or no headers)Let OPTIONS through without login and answer it with 204 + the allow headers
Request header field authorization is not allowed by Access-Control-Allow-HeadersYou send a header the server didn't listAdd it to Access-Control-Allow-Headers

Mistakes to avoid

  • Turning CORS off in your browser (extensions or startup flags) only fixes it on your computer. Your users will still see the error.
  • mode: "no-cors" in fetch doesn't bypass anything: it gives you an empty, unreadable response.
  • Reflecting any Origin back with credentials allowed is the same as allowing every website to act as your logged-in users. Keep an allow-list.
  • Thinking CORS protects your API. It protects users in browsers. Anyone can still call your API with curl, so you still need real authentication.

Key takeaways

  • An origin is scheme + host + port. Change any one and it's cross-origin.
  • Browsers block reading cross-origin replies to protect logged-in users: the same-origin policy.
  • CORS is the server's way of saying "this origin may read my replies", using Access-Control-Allow-Origin.
  • Some requests send an OPTIONS preflight first, which the server must answer.
  • Fix it on the server (or with a dev proxy), never by disabling the browser's check.

Quiz: test yourself

1. Are https://shop.com and https://api.shop.com the same origin?

No. The host is different, and a subdomain counts as a different host.

2. Why does the request work in Postman but not in the browser?

Because the same-origin policy is a browser rule that protects users' logged-in sessions. Postman and curl don't apply it.

3. Where do you fix a CORS error?

On the server being called, by sending Access-Control-Allow-Origin (and answering preflights), or by routing the call through your own backend or dev proxy.

4. Which requests trigger a preflight?

Ones that aren't "simple": for example PUT or DELETE, a JSON body, or custom headers like Authorization.

5. Can you use Access-Control-Allow-Origin: * with cookies?

No. With credentials the server must name the exact origin and send Access-Control-Allow-Credentials: true.

Further reading

New to APIs? Start with What Is an API?, then see how the browser finds the server in How DNS Works.

Two new visual guides every week

Plus animated tech explainers on YouTube and Facebook.

More from the Library