Request.mode

翻译不完整。 请帮助我们翻译这篇文章!

Request 接口的 mode 只读属性包含请求的模式(例如:corsno-corscors-with-forced-preflightsame-origin,或navigate。)这用于确定跨域请求是否能得到有效的响应,以及响应的哪些属性是可读的。

语法

var myMode = request.mode;

属性值

一个 RequestMode 值。

  • same-origin — 如果使用此模式向另外一个源发送请求,显而易见,结果会是一个错误。你可以设置该模式以确保请求总是向当前的源发起的。
  • no-cors — 保证其对应的方法只有 HEAD,GET 或 POST 方法。即使ServiceWorker 拦截了这个请求,除了 simple header 之外,不会添加或覆盖任意其他header, 另外JavaScript不会读取Response的任何属性 . 这样将会确保 ServiceWorker 不会影响Web语义(semantics of the Web), 同时保证了在跨域时不会发生安全和隐私泄露的问题.
  • cors — 允许跨域请求,例如访问第三方供应商提供的各种 API。These are expected to adhere to the CORS protocol. Only a limited set of headers are exposed in the Response, but the body is readable.
  • navigate — 表示这是一个浏览器的页面切换请求(request)。 navigate请求仅在浏览器切换页面时创建,该请求应该返回HTML。

默认模式

Requests can be initiated in a variety of ways, and the mode for a request depends on the particular means by which it was initiated.

For example, when a Request object is created using the Request.Request constructor, the value of the mode property for that Request is set to cors.

However, for requests created other than by the Request.Request constructor, no-cors is typically used as the mode; for example, for embedded resources where the request is initiated from markup, unless the crossorigin attribute is present, the request is in most cases made using the no-cors mode — that is, for the <link> or <script> elements (except when used with modules), or <img>, <audio>, <video>, <object>, <embed>, or <iframe> elements.

示例

In the following snippet, we create a new request using the 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

规范

规范 状态 备注
Fetch
mode
Living Standard Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
mode
Experimental
Chrome Full support 42Edge Full support 14Firefox Full support 39IE No support NoOpera Full support 29Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android Full support 5.0
navigate mode
Experimental
Chrome Full support 49Edge Full support ≤18Firefox Full support 46IE No support NoOpera ? Safari No support NoWebView Android No support NoChrome Android Full support 49Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android Full support 5.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

参见