Follow
Subscribe via Email!

Enter your email address to subscribe to this platform and receive notifications of new posts by email.

How CORS Works in Browsers for Cross-Origin API Requests

Browsers use Cross-Origin Resource Sharing headers and preflight OPTIONS checks to decide when scripts on one site can read data from another origin.
A dark diagram showing a URL, domain, protocol, cookies, and arrows between a bank and an attacker.

When a web page script tries to read data from another domain, scheme, or port, how CORS works comes down to an HTTP header handshake between the browser and the target server. Browsers block cross-origin script reads by default under the same-origin policy, so an app loaded from one site can’t inspect responses from another unless the remote server grants access [2][7]. That opt-in runs through headers like Origin and Access-Control-Allow-Origin under the WHATWG Fetch Living Standard [1][7].

Why CORS Is Needed for Browser Security

Browsers limit cross-origin reads because loaded pages carry ambient user credentials, including cookies, HTTP authentication, and TLS client certificates, whenever they send network requests [1][4][5]. A page can embed cross-origin images, stylesheets, scripts, iframes, and videos without extra headers, yet JavaScript APIs like fetch() and XMLHttpRequest obey same-origin boundaries by default [2][7]. Before browsers adopted Cross-Origin Resource Sharing, developers built server-side proxies to route Ajax calls through the same domain, which created open redirect bugs [6].

Today, browser checks cover far more than raw data calls, and how CORS works shapes how pages load media, fonts, and modules from outside hosts [1][2]. Under the Fetch Living Standard and Mozilla’s MDN Web Docs guide, browsers apply CORS to HTML elements carrying a crossorigin attribute such as <img crossorigin=”anonymous”>, JavaScript modules loaded via <script type=”module”> even when that attribute is omitted, CSS @font-face web fonts, CSS Shapes from images, WebGL textures, and images or video frames drawn onto a canvas via drawImage() so scripts can read pixel data without tainting the canvas [1]. Browser vendors have grown more cautious over time, so newer web APIs that initiate network calls generally enforce CORS even when older tags didn’t [2].

Origin boundaries also matter on shared hosts that separate accounts by folder path instead of hostname. The March 2009 W3C Working Draft warned authors using URLs such as people.example.org/~author-name/ that browser security isolates distinct origins, not distinct paths [4].

Origin Headers in CORS Requests

A basic GET call shows how CORS works between two separate origins. Suppose JavaScript on https://domain-a.com calls fetch() to read https://domain-b.com/data.json, or a page at https://foo.example requests JSON from https://bar.other over HTTP/1.1. In MDN’s sample trace from Firefox 71.0 on macOS, the browser adds Origin: https://foo.example to the outbound headers [2]. Unlike the Referer header, Origin omits URL paths that might leak private information, and it can carry the string “null” when a call originates from a local data: URL [4].

Control over access stays on the external server, never on the calling site [2]. In Wikipedia’s example between http://www.example.com and http://service.example.com, the service answers with an Access-Control-Allow-Origin header listing either the exact calling origin or the wildcard * for public assets like Google Fonts [7]. On https://bar.other, an Apache/2 server returning Access-Control-Allow-Origin: * opens the resource to any domain, whereas setting https://foo.example locks it down to one caller. If that header is missing or lists another domain, the browser withholds the payload from JavaScript [2][6]. Fetch also links this origin check with redirects, Content Security Policy, Service Workers, Mixed Content, and Referer rules [1].

Public GET responses without access checks can safely return *. The January 2014 W3C Recommendation noted that GET scripts parsed as ECMAScript can return * too if they contain no sensitive comments, because <script> tags already load them. Login pages shouldn’t send Access-Control-Allow-Origin [5].

Simple Requests

Not every cross-origin call triggers an extra round trip before data moves. Under the older W3C CORS label of “simple requests”—a term the Fetch standard no longer uses—browsers send the main HTTP call without checking first. That rule exists because HTML 4.0 <form> tags could already submit basic requests to any domain, so servers already had to defend against those submissions [2][5].

To skip preflight, a request must stay within narrow limits on its method, headers, and payload [2]. Only GET, HEAD, and POST count as CORS-safelisted methods [1]. Manually added headers are limited to Accept, Accept-Language, Content-Language, Content-Type, and a single Range header such as bytes=256- or bytes=127-255. Even Content-Type avoids preflight only when its media type is text/plain, multipart/form-data, or URL-encoded form data (application/x-www-form-urlencoded) [2][5]. If JavaScript attaches an event listener to xhr.upload.addEventListener() on XMLHttpRequest or sends a ReadableStream body, the browser promotes the call to a preflighted request [2].

Method casing also affects outcomes under the Fetch algorithm and RFC 7405 rules. Browsers uppercase DELETE, GET, HEAD, OPTIONS, POST, and PUT for consistency, while blocking CONNECT, TRACE, and TRACK as forbidden methods. Lowercase patch often returns a 405 Method Not Allowed error, while uppercase PATCH succeeds [1].

Preflight OPTIONS Checks

When a script calls PUT or DELETE, uses Content-Type: text/xml, or sets a custom header like X-PINGOTHER, the browser pauses before sending the main payload [2][4]. It issues an HTTP OPTIONS probe, called a preflight request, to check how CORS works for unsafe methods or custom headers on that server [2].

During that preflight step, the browser sends Origin alongside Access-Control-Request-Method and Access-Control-Request-Headers—such as POST with content-type,x-pingother in MDN’s XML example (<person><name>Arun</name></person>) or POST with NCZ in Nicholas C. Zakas’s blog post [2][6]. On https://bar.other/doc, the Apache/2 server answers with HTTP 204 No Content and returns Access-Control-Allow-Origin: https://foo.example, Access-Control-Allow-Methods: POST, GET, OPTIONS, and Access-Control-Allow-Headers: X-PINGOTHER, Content-Type. Once the browser validates those headers, it sends the real POST call without the Access-Control-Request-* probe headers [2]. If the server rejects the OPTIONS check, the browser blocks the main request from running [7].

Caching cuts down on repeat preflight round trips. Servers return Access-Control-Max-Age with a duration in seconds, such as 86400 in MDN’s guide, 1728000 in Zakas’s example, or 3628800 in the W3C drafts [2][4][6]. Within that time window, the browser skips extra OPTIONS probes, while Vary: Accept-Encoding, Origin tells caches to separate replies by origin [2][4].

Human Who Codes preview card for Nicholas C. Zakas's guide on how CORS works in browsers.
Nicholas C. Zakas’s May 2010 Human Who Codes post explained how browsers handle cross-domain Ajax requests with CORS headers. (Credit: Human Who Codes)

Credentials and Cookies

Cross-origin fetch() and XMLHttpRequest calls omit user credentials by default [6][7]. In both Fetch and the 2014 W3C Recommendation, user credentials cover cookies, HTTP authentication, and client-side SSL or TLS certificates, excluding proxy authentication and the Origin header [1][5]. A script that needs authenticated cookies on XMLHttpRequest must set withCredentials to true [6].

Client opt-in alone never unlocks credentialed endpoints. The server must respond with Access-Control-Allow-Credentials: true, and it must echo an exact origin in Access-Control-Allow-Origin instead of using the * wildcard [2][6][7]. If a credentialed call comes back without Access-Control-Allow-Credentials: true, the browser blocks JavaScript from reading the reply, leaving responseText empty and status at 0 while firing onerror() [6]. Even when a request succeeds, scripts can only read simple response headers—Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, and Pragma—unless the server lists additional fields in Access-Control-Expose-Headers [5][7].

This permission check solves a different problem than cross-site request forgery protection. CORS stops external scripts from reading private responses, while forged requests exploit cookies attached to form posts or simple calls that trigger no preflight. As the 2014 W3C Recommendation notes, endpoints that change server state still need unguessable tokens inside the request content [5].

Why CORS Errors Occur

Developers see why CORS error occurs most often when a cross-origin reply lacks a matching Access-Control-Allow-Origin header, fails an OPTIONS preflight check, or pairs credentials with a wildcard origin [2]. Sometimes that block reflects an intentional restriction set by the remote service, not a server bug [3]. Either way, browsers hide failure specifics from JavaScript so scripts can’t inspect why a remote endpoint rejected them [2][3].

Developers tracing how CORS works when a request fails start in the browser’s Developer Tools console, not in script logs [2]. Firefox prints messages beginning with “Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource” plus a Reason note naming the failed rule. Because the remote server sets the policy headers, most fixes happen on the server. When a team can’t edit the target API, MDN’s CORS errors guide lists three client-side options: simplify the call to avoid preflight, use mode: “no-cors” in fetch(), or route traffic through a proxy server [3].

The mode: “no-cors” option works for analytics beacons or Service Worker caching, where script code doesn’t need to read the response. It returns an opaque reply with status 0, empty headers, and an unreadable body, while a self-hosted proxy restores full headers at the cost of extra latency and proxy upkeep [3].

From VoiceXML to the Fetch Standard

Cross-origin resource sharing started in voice browsing before reaching web pages. In March 2004, Matt Oshry, Brad Porter, and Michael Bodell of Tellme Networks proposed the model for VoiceXML 2.1 so voice browsers could safely fetch remote data [7]. Because the design fit other web APIs too, the W3C turned it into an implementation note and assigned it to the WebApps Working Group under the Rich Web Clients Activity in the W3C Interaction Domain, yielding the first Working Draft in May 2006 [4][7].

The W3C draft carried three earlier names—Access Control for Cross-Site Requests, Enabling Read Access for Web Resources, and Authorizing Read Access to XML Content Using the <?access-control?> Processing Instruction 1.0—before the March 2009 draft took the title Cross-Origin Resource Sharing [4][7]. That specification targeted hosting specs such as XMLHttpRequest Level 2, XBL 2.0, and HTML5, and later Server-Sent Events, while basing its header grammar on RFC 2616 and RFC 2119 [4]. In January 2014, WebApps and WebAppSec published it as a W3C Recommendation noting compatibility with HTTP status code 308 [5][7]. Today, the WHATWG Fetch Living Standard defines how CORS works in web browsers, built on the Infra Standard and superseding the standalone W3C text [1][7].

Before CORS spread, developers relied on JSONP to pull cross-domain data in legacy browsers like Opera Mini and Internet Explorer 9. CORS replaced JSONP because it supports HTTP methods beyond GET, gives XMLHttpRequest proper error events, and lets sites parse responses manually instead of running third-party scripts that risk XSS attacks [7].

Browser Engine Support

All major desktop and mobile browser engines have supported CORS widely since July 2015, after years of earlier rollouts [2]. Blink and Chromium support spans Google Chrome 28+, Opera 15+, Amazon Silk, Android 4.4+ WebView, and Qt’s WebEngine. Gecko 1.9.1 added support in Firefox 3.5 and SeaMonkey 2.0, WebKit shipped it by Safari 4 and Google Chrome 3, and Presto added it in Opera 12.00 and Opera Mobile 12 [7].

Microsoft added CORS in two phases. Internet Explorer 8 and 9 (MSHTML/Trident 4.0 and 5.0) offered partial support only through Microsoft’s XDomainRequest object, which handled simple GET and POST calls without preflight checks or withCredentials [6][7]. In May 2010, Zakas credited Mozilla’s guidance and Arun Ranganathan’s examples page when sharing a createCORSRequest helper that checked “withCredentials” in xhr before falling back to XDomainRequest [6]. Native XMLHttpRequest support arrived in Internet Explorer 10 (Trident 6.0), and all versions of Microsoft Edge support CORS, while Opera Mini still lacks support [7].

Minor differences remain between browser engines on how CORS works for safelisted headers. WebKit Nightly and Safari Technology Preview trigger a preflight request when Accept, Accept-Language, or Content-Language hold values those browsers treat as nonstandard, behavior logged in WebKit bug reports. Other engines skip that extra preflight because the Fetch spec doesn’t ask for it [2].

Sources
  1. WEBSITE WHATWG. (2026, November 30). Fetch Living Standard. WHATWG. [Article Link]
  2. WEBSITE MDN Web Docs. (2026, October 28). Cross-Origin Resource Sharing (CORS). Mozilla. [Article Link]
  3. WEBSITE MDN Web Docs. (2026, October 16). CORS errors. Mozilla. [Article Link]
  4. REPORT World Wide Web Consortium (W3C). (2009, March 17). Cross-Origin Resource Sharing (W3C Working Draft). W3C. [Article Link]
  5. REPORT World Wide Web Consortium (W3C). (2014, January 16). Cross-Origin Resource Sharing (W3C Recommendation). W3C. [Article Link]
  6. WEBSITE Zakas, N. C. (2010, May 25). Cross-domain Ajax with Cross-Origin Resource Sharing. Human Who Codes. [Article Link]
  7. WEBSITE Wikipedia. (n.d.). Cross-origin resource sharing. Wikipedia. [Article Link]

Cover image: FirstPageSEOGroup.com / Wikimedia Commons, CC BY-SA 4.0

Leave a Comment

Related Posts
Total
0
Share