ByteTips.

Guide

How to Fix 'CORS policy: No Access-Control-Allow-Origin header'

CORS is the browser blocking your fetch, not the server rejecting it. If you control the API, add the right header; if you don't, proxy through your own backend — never disable browser security.

October 24, 2026 · 6 min read · Shayan · JavaScript

Short answer: CORS (Cross-Origin Resource Sharing) is enforced by the browser, not by the server. When your JavaScript at https://app.example.com fetches https://api.other.com and the API's response lacks an Access-Control-Allow-Origin header, the browser refuses to hand the response to your code — even though the request itself succeeded. The fix depends on one question: do you control the API? If yes, send the header. If no, stop calling the API directly from the browser and route the request through your own backend.

What CORS actually blocks

The same-origin policy says a page may only read responses from its own origin (same scheme, host, and port). CORS is the escape hatch: a server opts in to cross-origin reads by sending Access-Control-Allow-Origin. Without it, the browser still sends the request — your server logs will show it arriving — but the response is quarantined and your fetch rejects with a TypeError. That is why curl and Postman work fine while the browser fails: curl does not enforce CORS at all. A working curl request proves the API is alive; it proves nothing about browser access.

Simple requests vs preflight

Not every request gets the same treatment. A simple request — GET, HEAD, or POST with only safelisted headers and a safelisted content type (text/plain, application/x-www-form-urlencoded, or multipart/form-data) — goes straight through, and the browser checks the header on the response. Anything else triggers a preflight: the browser first sends an OPTIONS request asking permission, and only sends the real request if the preflight response allows the method and headers. This is the trap most people fall into: you add Access-Control-Allow-Origin to your GET handler, but the preflight OPTIONS handler does not send it, so requests with a JSON body or an Authorization header still fail. Both responses need the headers, and the OPTIONS response must also include Access-Control-Allow-Methods and Access-Control-Allow-Headers.

If you control the API

  • Send Access-Control-Allow-Origin with the exact calling origin (https://app.example.com), not a wildcard, whenever the request uses credentials. Browsers reject a wildcard origin combined with credentials outright.
  • Handle OPTIONS explicitly and return 204 with Access-Control-Allow-Origin, Access-Control-Allow-Methods (GET, POST, PUT, DELETE, whatever you actually support), and Access-Control-Allow-Headers (Content-Type, Authorization, and any custom header the client sends).
  • If the client sends credentials (cookies, Authorization), add Access-Control-Allow-Credentials: true and set fetch's credentials option to 'include' — and remember the origin must then be specific, never *.
  • Add Vary: Origin to responses when the allowed origin varies per request, so shared caches do not serve one origin's header to another.

If you don't control the API

You cannot fix this from the frontend — no header, fetch option, or mode setting overrides the browser's enforcement, and mode: 'no-cors' only gives you an opaque response you cannot read, which is useless for JSON APIs. The correct fix is a same-origin proxy: your frontend calls your own backend (same origin, so no CORS), and your backend calls the third-party API (server-to-server, where CORS does not apply). In development, the framework dev-server proxy does the same job — Vite's server.proxy, Next.js rewrites, or webpack's devServer.proxy all forward /api/* to the real API without the browser ever seeing a cross-origin request.

What not to do

  • Do not install a browser extension that disables CORS and call the bug fixed — it only fixes your machine, and it disables a security boundary for every site you visit.
  • Do not route production traffic through a public CORS proxy: your users' requests, and any tokens in them, flow through a server you do not control.
  • Do not set Access-Control-Allow-Origin: * on an API that accepts credentials or serves private data — the wildcard is only safe for genuinely public, unauthenticated resources.

Why does curl work when the browser fails?

Because curl does not enforce CORS at all — it sends the request and shows you whatever comes back, with no origin check. The browser is the only party enforcing the policy, which is why a working curl request proves the API is reachable but says nothing about whether a web page is allowed to read its responses.

Written by Shayan — Creator of Byte-Kit & ByteTips. Building private developer tools.

More JavaScript guides