Request: mode property
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
* Some parts of this feature may have varying levels of support.
Note: This feature is available inWeb Workers.
Themode read-only property of theRequestinterface contains the mode of the request (e.g.,cors,no-cors,same-origin, ornavigate.) This is usedto determine if cross-origin requests lead to valid responses, and which properties of the response are readable.
To construct a request with a specific mode, pass the desired value as theRequestInit.mode option to theRequest.Request() constructor.
Note that setting particular modes, especiallyno-cors, places restrictions on the request methods and headers that may be used, and prevents JavaScript from accessing the response headers or body. See the documentation forRequestInit.mode for more details.
In this article
Value
One of the following values:
same-originDisallows cross-origin requests. If a request is made to another origin with this mode set, the result is an error.
no-corsDisables CORS for cross-origin requests. The response isopaque, meaning that its headers and body are not available to JavaScript.
corsIf the request is cross-origin then it will use theCross-Origin Resource Sharing (CORS) mechanism.
navigateA mode for supporting navigation. The
navigatevalue is intended to be used only by HTML navigation. A navigate request is created only while navigating between documents.
Default mode
Requests can be initiated in a variety of ways, and the mode for a request depends onthe particular means by which it was initiated.
For example, when aRequest object is created using theRequest() constructor, the value of themode propertyfor thatRequest is set tocors.
However, for requests created other than by theRequest()constructor,no-cors is typically used as the mode; for example, forembedded resources where the request is initiated from markup, unless thecrossoriginattribute is present, the request is in most cases made using theno-corsmode — that is, for the<link> or<script> elements(except when used with modules), or<img>,<audio>,<video>,<object>,<embed>, or<iframe> elements.
Examples
In the following snippet, we create a new request using theRequest() constructor (for an image file in the same directory asthe script), then save the request mode in a variable:
const myRequest = new Request("flowers.jpg");const myMode = myRequest.mode; // returns "cors" by defaultSpecifications
| Specification |
|---|
| Fetch> # ref-for-dom-request-mode②> |