This
cross-origin sharing standard
can enable cross-origin HTTP requests for:
Invocations of
fetch()
or
XMLHttpRequest
, as discussed above.
Web Fonts (for cross-domain font usage in
@font-face
within CSS), as described in the
font fetching requirements
, so that servers can deploy TrueType fonts that can only be loaded cross-origin and used by websites that are permitted to do so.
WebGL textures
Images/video frames drawn to a canvas using
drawImage()
CSS Shapes from images.
This is a general article about Cross-Origin Resource Sharing and includes a discussion of the necessary HTTP headers.
The Cross-Origin Resource Sharing standard works by adding new
HTTP headers
that let servers describe which origins are permitted to read that information from a web browser. Additionally, for HTTP request methods that can cause side-effects on server data (in particular, HTTP methods other than
GET
, or
POST
with certain
MIME types
), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with the HTTP
OPTIONS
request method, and then, upon "approval" from the server, sending the actual request. Servers can also inform clients whether "credentials" (such as
and
HTTP Authentication
) should be sent with requests.
CORS failures result in errors but for security reasons, specifics about the error
are not available to JavaScript
. All the code knows is that an error occurred. The only way to determine what specifically went wrong is to look at the browser's console for details.
Subsequent sections discuss scenarios, as well as provide a breakdown of the HTTP headers used.
We present three scenarios that demonstrate how Cross-Origin Resource Sharing works. All these examples use
fetch()
, which can make cross-origin requests in any supporting browser.
Some requests don't trigger a
CORS preflight
. Those are called
simple requests
from the obsolete
CORS spec
, though the
Fetch spec
(which now defines CORS) doesn't use that term.
The motivation is that the
US