Skip to content

Why does my fetch to another origin fail with a CORS error?

IntermediateAsked very oftenConcept roundDebuggingBrowser
Asked atStripeShopifyMicrosoft
#cors#http#headers#browser-security#preflight

Company tags are compiled from public interview reports and are signals, not guarantees.

What interviewers are testing

CORS questions separate people who copied "add cors() middleware" from people who understand the browser as an enforcing client. Interviewers listen for three things: that the browser — not the server — blocks the read, that non-simple requests pay an OPTIONS preflight before the real call, and that the fix is response headers or a same-origin proxy, never a client-side trick.

Mental model

The browser is a bouncer inside the club: requests walk out freely, but a response only comes back to JavaScript if the other origin explicitly signs the guest list (Access-Control-Allow-Origin). Non-simple calls must first send a scout (OPTIONS) to confirm the guest list covers the method and headers — and the approval ticket is cached until it expires.

Step-by-step solution

  1. Step 1: The browser, not the server, blocks the read

    The first surprise: the request is not stopped. It travels to the other origin, the server processes it and returns a perfectly normal 200 with JSON. Then the browser inspects the response and — if there is no Access-Control-Allow-Origin header matching the page's origin — it withholds the body from JavaScript. Your catch block sees a generic TypeError: Failed to fetch, while the Network tab shows a completed request. Watch the animation: the packet reaches the API and comes back, yet the last mile into the page is where the read is denied. This is why CORS exists at all: without it, any evil.example page you visit could silently call your bank's API and read the authenticated response using your cookies.

  2. Step 2: Simple or non-simple: what triggers the OPTIONS call

    Not every cross-origin request pays a preflight. The specification defines a "simple" shape — GET, HEAD or POST, only safelisted headers, and Content-Type restricted to text/plain, multipart/form-data or application/x-www-form-urlencoded. Anything else is non-simple: a PUT or DELETE, a custom header like Authorization, or the everyday Content-Type: application/json. Non-simple requests cannot leave immediately; the browser must first ask the other origin for permission, because a blind request could already have side effects — a DELETE that fires before the browser decides the response may not be read is too late to undo. Compare the two panels: the left column is sent straight through, the right column is gated.

  3. Step 3: The OPTIONS handshake

    The preflight is an HTTP request the browser composes on its own — you cannot opt out of it. It sends OPTIONS with Access-Control-Request-Method and Access-Control-Request-Headers describing exactly what the real request intends to do. The server must reply with Access-Control-Allow-Origin, -Methods and -Headers that cover it. Only then does the browser release the actual request. The approval is cached for the duration of Access-Control-Max-Age, which is why the OPTIONS row often disappears from the Network tab on refresh. Watch the sequence: negotiate, cache, then the real PUT travels — a preflight is a round trip, so it is latency worth understanding and worth caching.

  4. Step 4: The response headers that grant access

    Every granting header carries one rule that trips people up. Allow-Origin must match the page origin exactly — the wildcard * works only for requests without credentials, and with cookies or Authorization it is rejected outright; the server has to echo the exact origin and add Access-Control-Allow-Credentials: true. Allow-Methods and Allow-Headers must name everything the real request uses, and one missing entry fails the whole preflight even if the rest is perfect. Max-Age is the performance lever most teams forget: with it, the browser stops asking on every call. Read the response headers like a contract — each line is one clause of what the browser is about to permit.

  5. Step 5: Debugging it in sixty seconds

    Treat the client error as a symptom, not a diagnosis. Open the Network tab and look for the OPTIONS row: if it is red, missing, or answered 404/405, the API is refusing to negotiate and the real request never ran. If OPTIONS is green but the real request is flagged, compare the granted headers against what the request actually sends — a missing method or header entry is the usual culprit. Then fix it where it lives: server-side middleware or gateway configuration, never in the fetch options, because the client cannot lift a browser-enforced block. When you do not control the API, proxy the call through your own origin — a Next.js rewrite or nginx location — and the browser never sees a cross-origin request at all.

Edge cases & traps

  • A failed preflight means the real request never happens: the browser aborts before your PUT exists on the network, so the server logs can look empty even though the client "sent" it.
  • The wildcard ACAO value (*) is incompatible with credentialed requests — cookies or Authorization: include require the exact origin plus Access-Control-Allow-Credentials: true.
  • CORS failures surface as a generic TypeError in JavaScript: the real reason is only in the console and the Network tab, so never debug them from the catch block alone.
  • A 500 from the API can masquerade as a CORS failure when the error response is missing the ACAO header — the browser hides even the status code from JavaScript.
  • The preflight cache is keyed per URL, method and header set, and browsers cap Max-Age (Chrome around two hours), so "it worked yesterday" can still mean a fresh OPTIONS today.

Follow-up questions

Go deeper: Explore the API visualizer