Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1072250 of Request.mode

  • Revision slug: Web/API/Request/mode
  • Revision title: Request.mode
  • Revision id: 1072250
  • Created:
  • Creator: jsx
  • Is current revision? Yes
  • Comment Revert to revision of 2016-05-29 08:33:24 by Rob W: "Reverting translation that overwrote English content. To learn how to translate, please see https://developer.mozilla.org/en-US/docs/MDN/Contribute/Localize/Translating_pages"

Revision Content

{{APIRef("Fetch")}}{{SeeCompatTable}}

The mode read-only property of the {{domxref("Request")}} interface contains the mode of the request (e.g., cors, no-cors, cors-with-forced-preflight, same-origin, or navigate.) This is used to determine if cross-origin requests lead to valid responses, and which properties of the response are readable:

  • same-origin — If a request is made to another origin with this mode set, the result is simply an error. You could use this to ensure that a request is always being made to your origin.
  • no-cors — Prevents the method from being anything other than HEAD, GET or POST. If any ServiceWorkers intercept these requests, they may not add or override any headers except for these. In addition, JavaScript may not access any properties of the resulting {{domxref("Response")}}. This ensures that ServiceWorkers do not affect the semantics of the Web and prevents security and privacy issues arising from leaking data across domains.
  • cors — Allows cross-origin requests, for example to access various APIs offered by 3rd party vendors. These are expected to adhere to the CORS protocol. Only a limited set of headers are exposed in the {{domxref("Response")}}, but the body is readable.
  • navigate — A mode for supporting navigation. The navigate value is intended to be used only by HTML navigation. A navigate request is created only while navigating between documents.

Syntax

var myMode = request.mode;

Value

A {{domxref("RequestMode")}} value.

Example

In the following snippet, we create a new request using the {{domxref("Request.Request()")}} constructor (for an image file in the same directory as the script), then save the request mode in a variable:

var myRequest = new Request('flowers.jpg');
var myMode = myRequest.mode; // returns "cors" by default

Specifications

Specification Status Comment
{{SpecName('Fetch','#dom-request-mode', 'mode')}} {{Spec2('Fetch')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatChrome(42.0)}} {{CompatGeckoDesktop(39)}} {{CompatNo}}

29
28[1]

{{CompatNo}}
navigate mode {{CompatChrome(49.0)}} {{CompatGeckoDesktop(46)}} {{CompatNo}} {{CompatUnknown}} {{CompatNo}}
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatChrome(49.0)}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatChrome(49.0)}}
navigate mode {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatChrome(49.0)}}

See also

Revision Source

<div>{{APIRef("Fetch")}}{{SeeCompatTable}}</div>

<p>The <strong><code>mode</code></strong> read-only property of the {{domxref("Request")}} interface contains the mode of the request (e.g., <code>cors</code>, <code>no-cors</code>, <code>cors-with-forced-preflight</code>, <code>same-origin</code>, or <code>navigate</code>.) This is used to determine if cross-origin requests lead to valid responses, and which properties of the response are readable:</p>

<ul>
 <li><code>same-origin</code> — If a request is made to another origin with this mode set, the result is simply an error. You could use this to ensure that a request is always being made to your origin.</li>
 <li><code>no-cors</code> — Prevents the method from being anything other than <code>HEAD</code>, <code>GET</code> or <code>POST</code>. If any ServiceWorkers intercept these requests, they may not add or override any headers except for <a href="https://fetch.spec.whatwg.org/#simple-header">these</a>. In addition, JavaScript may not access any properties of the resulting {{domxref("Response")}}. This ensures that ServiceWorkers do not affect the semantics of the Web and prevents security and privacy issues arising from leaking data across domains.</li>
 <li><code>cors</code> — Allows cross-origin requests, for example to access various APIs offered by 3rd party vendors. These are expected to adhere to the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS">CORS protocol</a>. Only a <a href="https://fetch.spec.whatwg.org/#concept-filtered-response-cors">limited set</a> of headers are exposed in the {{domxref("Response")}}, but the body is readable.</li>
 <li><code>navigate</code> — A mode for supporting navigation. The <code>navigate</code> value is intended to be used only by HTML navigation. A navigate request is created only while navigating between documents.</li>
</ul>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
var <var>myMode</var> = <var>request</var>.mode;</pre>

<h3 id="Value">Value</h3>

<p>A {{domxref("RequestMode")}} value.</p>

<h2 id="Example">Example</h2>

<p>In the following snippet, we create a new request using the {{domxref("Request.Request()")}} constructor (for an image file in the same directory as the script), then save the request mode in a variable:</p>

<pre class="brush: js" data-number="">
var myRequest = new Request('flowers.jpg');
var myMode = myRequest.mode; // returns "cors" by default</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Fetch','#dom-request-mode', 'mode')}}</td>
   <td>{{Spec2('Fetch')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(42.0)}}</td>
   <td>{{CompatGeckoDesktop(39)}}</td>
   <td>{{CompatNo}}</td>
   <td>
    <p>29<br />
     28<sup>[1]</sup></p>
   </td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>navigate</code> mode</td>
   <td>{{CompatChrome(49.0)}}</td>
   <td>{{CompatGeckoDesktop(46)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49.0)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49.0)}}</td>
  </tr>
  <tr>
   <td><code>navigate</code> mode</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49.0)}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li>
 <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li>
 <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li>
</ul>
Revert to this revision