A script running on youtube.com asked example.com for its home page, and the browser answered with a two-word error: “Failed to fetch.” Run the same request from a terminal with curl and the page comes back without complaint. Nothing was wrong with the network, and example.com wasn’t refusing anyone. The block happened inside the browser, after the response had arrived, because the response didn’t say that a page from another origin was allowed to read it. That rule is the same-origin policy, and CORS is the system of HTTP headers that relaxes it. The tests below ran in a real Chrome tab and against a small Python server built for this post.
Origins and the same-origin policy
What counts as an origin
An origin is the combination of scheme, host and port. MDN’s CORS guide spells it out as “domain, scheme, or port.” https://example.com and http://example.com are different origins. So are http://localhost:3000 and http://localhost:8080, which is why the error shows up so often in local development: a front end on one port calling an API on another counts as cross-origin.
Same site or not, at a glance
Applying that scheme-host-port rule to a page at https://example.com/page gives these results:
| Request to | Same origin? | Why |
|---|---|---|
https://example.com/api/data |
Yes | Only the path differs |
http://example.com/api/data |
No | Different scheme |
https://api.example.com/data |
No | Different host, even as a subdomain |
https://example.com:8443/data |
No | Different port |
The subdomain row catches a lot of teams. A site on www calling its own API on an api subdomain is making a cross-origin request, and needs CORS headers like any outside caller would. The path never matters; everything before it does.
The default is no
The guide explains the baseline: “fetch() and XMLHttpRequest follow the same-origin policy.” A web application using them “can only request resources from the same origin the application was loaded from unless the response from other origins includes the right CORS headers.” Without that permission, a page that someone happens to have open can’t use their browser, and the logins stored in it, to quietly read data from other sites.
What CORS adds
MDN defines CORS as “an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.” The permission comes from the server being called. The page making the call has no say.

The test: one browser, three requests
A blocked read
From a Chrome tab on https://www.youtube.com, a script ran fetch('https://example.com/'). It threw a TypeError with the message “Failed to fetch.” The script got no status code, no headers and no body, nothing it could inspect to learn why.
An allowed read
The same tab fetched https://api.github.com/zen. That returned status 200 and the body, because GitHub’s API sends CORS headers that permit it.
A request that succeeds but reveals nothing
Calling example.com again with {mode: 'no-cors'} didn’t throw. It returned a response of type opaque with a status of 0. The browser sent the request and got an answer, but handed the script an empty shell. That mode exists for loading resources a page doesn’t need to read. It isn’t a workaround for a CORS error, and code that “fixes” a CORS problem this way ends up with a response it can’t use.
The headers that grant permission
Access-Control-Allow-Origin
The key response header names the origin allowed to read the response, or * for any origin. A test API, written in Python’s http.server, was set to allow exactly one origin, http://app.localhost:8080. A request carrying that Origin header got back:
HTTP/1.0 200 OK
Access-Control-Allow-Origin: http://app.localhost:8080
Vary: Origin
Content-Type: application/json
Vary: Origin
That second header matters whenever a server picks from a list. MDN says that if the server “specifies a single origin (that may dynamically change based on the requesting origin as part of an allowlist) rather than the * wildcard,” it “should also include Origin in the Vary response header.” Without it, a cache in the middle could store the response permitting one site and hand it to a request from another.
The server answered anyway
A request from an origin not on the list, http://evil.localhost:8080, got a 200 status and the full {"ok":true} body, just without the CORS header. The server never refused anything. CORS headers instruct the browser; they don’t stop curl, a script or another server from reading the response.

Preflight requests
When the browser asks first
Not every request goes straight through. MDN describes a “simple request” as one using GET, HEAD or POST, with only a short list of safelisted headers, and a Content-Type limited to application/x-www-form-urlencoded, multipart/form-data or text/plain. Anything else, a PUT, a DELETE, a JSON body, an Authorization header, is “preflighted”: the browser “first sends an HTTP request using the OPTIONS method to the resource on the other origin, in order to determine if the actual request is safe to send.”
What the preflight asks
The OPTIONS request carries Access-Control-Request-Method and, if needed, Access-Control-Request-Headers, announcing what the real request will look like. Sending one to the Python test server for a PUT with a content-type header returned:
HTTP/1.0 204 No Content
Access-Control-Allow-Origin: http://app.localhost:8080
Vary: Origin
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 600
The browser checks that answer, and only if it covers the method and headers does it send the actual PUT.
Caching the answer
Access-Control-Max-Age “indicates how long the results of a preflight request can be cached,” in seconds, according to MDN. With 600, the browser can skip the extra round trip for ten minutes. Browsers may cap the value at their own lower limit.
The failure that looks like a server bug
In the browser test, a PUT to the GitHub endpoint with a custom X-Test header failed with the same “Failed to fetch,” even though a plain GET to the same URL had worked moments earlier. From the script’s side, a rejected preflight and an unreachable server look identical. That’s the most confusing part of CORS: a request that works in curl or Postman can fail in the browser purely because of a method or header the server’s preflight answer didn’t allow.
Credentials and the wildcard
Cookies change the rules
By default, a cross-origin fetch doesn’t send cookies. When a request includes credentials, MDN states that the server “must not specify the * wildcard for the Access-Control-Allow-Origin response-header value, but must instead specify an explicit origin.” A response to a logged-in request has to name exactly who may read it.
Why that rule exists
A wildcard on a public, anonymous resource, like a font or an open API, is harmless: anyone may read it, and nothing personal is in it. A wildcard on a response tied to a user’s session would let any site read that user’s private data through their own browser. The ban on combining the two closes that gap at the specification level.
Reflecting the Origin header is not a fix
A common shortcut copies whatever arrives in the Origin header straight into Access-Control-Allow-Origin. It silences the error, and it gives every website on the internet the same access as the site the API was built for. The Python test server checked each origin against an explicit allowlist for that reason.

Reading the error and fixing it in the right place
The script sees less than the developer
The TypeError a script catches carries no detail. The browser’s developer tools, in the Console and Network panels, show the blocked request and, usually, which header was missing or which preflight check failed. That’s where the actual diagnosis lives.
Headers the script can’t see
A detail from the GitHub test explains another common puzzle. The request succeeded, yet response.headers.get('access-control-allow-origin') returned null. Listing the headers showed only content-length, content-type and a set of rate-limit headers GitHub chose to expose. MDN’s page on Access-Control-Expose-Headers explains why: by default scripts see only the CORS-safelisted response headers, “Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified, Pragma.” Anything else must be listed by the server in Access-Control-Expose-Headers.
The fix belongs on the server
Because the permission comes from the responding server, the real fix is there: send the right Access-Control-Allow-Origin for the origins that should have access, answer OPTIONS preflights with the methods and headers actually used, add Vary: Origin when choosing from a list, and expose any custom headers the front end needs. When the API belongs to someone else and doesn’t allow browser access, the usual path is to call it from a server the developer controls, where the same-origin policy doesn’t apply, and have the page call that server instead.
In development
Many front-end development servers can proxy API calls so the browser sees a single origin. That avoids the error locally without loosening anything in production, which is where a permissive CORS setting would actually expose users.
Sources
- Cross-Origin Resource Sharing (CORS), MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS
- Access-Control-Expose-Headers, MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Expose-Headers
