Request

Request interface of the 抓取 API represents a resource request.

You can create a new Request object using the Request() constructor, but you are more likely to encounter a Request object being returned as the result of another API operation, such as a service worker FetchEvent.request .

构造函数

Request()
创建新的 Request 对象。

特性

Request.cache 只读
Contains the cache mode of the request (e.g., default , reload , no-cache ).
Request.context 只读
Contains the context of the request (e.g., audio , image , iframe , etc.)
Request.credentials 只读
Contains the credentials of the request (e.g., omit , same-origin , include ). The default is same-origin .
Request.destination 只读
Returns a string from the RequestDestination enum describing the request's destination. This is a string indicating the type of content being requested.
Request.headers 只读
Contains the associated Headers object of the request.
Request.integrity 只读
Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE= ).
Request.method 只读
Contains the request's method ( GET , POST , etc.)
Request.mode 只读
Contains the mode of the request (e.g., cors , no-cors , same-origin , navigate .)
Request.redirect 只读
Contains the mode for how redirects are handled. It may be one of follow , error ,或 manual .
Request.referrer 只读
Contains the referrer of the request (e.g., client ).
Request.referrerPolicy 只读
Contains the referrer policy of the request (e.g., no-referrer ).
Request.url 只读
Contains the URL of the request.

Request implements Body , so it also inherits the following properties:

body 只读
A simple getter used to expose a ReadableStream of the body contents.
bodyUsed 只读
Stores a Boolean that declares whether the body has been used in a response yet.

方法

Request.clone()
Creates a copy of the current Request 对象。

Request implements Body , so it also has the following methods available to it:

Body.arrayBuffer()
Returns a promise that resolves with an ArrayBuffer representation of the request body.
Body.blob()
Returns a promise that resolves with a Blob representation of the request body.
Body.formData()
Returns a promise that resolves with a FormData representation of the request body.
Body.json()
Returns a promise that resolves with a JSON representation of the request body.
Body.text()
Returns a promise that resolves with an USVString (text) representation of the request body.

注意 : The Body functions can be run only once; subsequent calls will resolve with empty strings/ArrayBuffers.

范例

In the following snippet, we create a new request using the Request() constructor (for an image file in the same directory as the script), then return some property values of the request:

const request = new Request('https://www.mozilla.org/favicon.ico');
const url = request.url;
const method = request.method;
const credentials = request.credentials;
					

You could then fetch this request by passing the Request object in as a parameter to a WindowOrWorkerGlobalScope.fetch() call, for example:

fetch(request)
  .then(response => response.blob())
  .then(blob => {
    image.src = URL.createObjectURL(blob);
  });
					

In the following snippet, we create a new request using the Request() constructor with some initial data and body content for an api request which need a body payload:

const request = new Request('https://example.com', {method: 'POST', body: '{"foo": "bar"}'});
const url = request.url;
const method = request.method;
const credentials = request.credentials;
const bodyUsed = request.bodyUsed;
					

注意: The body type can only be a Blob , BufferSource , FormData , URLSearchParams , USVString or ReadableStream type, so for adding a JSON object to the payload you need to stringify that object.

You could then fetch this api request by passing the Request object in as a parameter to a WindowOrWorkerGlobalScope.fetch() call, for example and get the response:

fetch(request)
  .then(response => {
    if (response.status === 200) {
      return response.json();
    } else {
      throw new Error('Something went wrong on api server!');
    }
  })
  .then(response => {
    console.debug(response);
    // ...
  }).catch(error => {
    console.error(error);
  });
					

规范

规范 状态 Comment
Fetch
The definition of 'Request' in that specification.
实时标准 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Request
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ≤18 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 28 Safari 10.1 WebView Android 42 Chrome Android 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android 28 Safari iOS 10.3 Samsung Internet Android 4.0
Request() 构造函数
Chrome 41
41
From Chrome 47, default values for the init argument's properties changed. mode 默认为 same-origin (from no-cors ). credentials 默认为 include (from same-origin ). redirect 默认为 follow (from manual ).
Edge 15 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42
42
From WebView 47, default values for the init argument's properties changed. mode 默认为 same-origin (from no-cors ). credentials 默认为 include (from same-origin ). redirect 默认为 follow (from manual ).
Chrome Android 41
41
From Chrome 47, default values for the init argument's properties changed. mode 默认为 same-origin (from no-cors ). credentials 默认为 include (from same-origin ). redirect 默认为 follow (from manual ).
Firefox Android 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
5.0
Some default values for the init parameter changed in Samsung Internet 5.0. See the Properties section for details.
cache
Chrome 64 Edge 14 Firefox 48 IE No Opera 51 Safari 11 WebView Android 64 Chrome Android 64 Firefox Android No Opera Android 47 Safari iOS No Samsung Internet Android 9.0
clone
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android No Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android No
context
弃用 非标
Chrome 42 — 46 Edge ? Firefox 39 — 42
不支持 39 — 42
see bug 1188062 了解更多信息。
IE No Opera 28 — 29 Safari No WebView Android 42 — 46 Chrome Android 42 — 46 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android 4.0 — 5.0
credentials
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42 Chrome Android 42 Firefox Android Yes Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS 10.3 Samsung Internet Android 4.0
destination
Chrome 65 Edge 14 Firefox 61 IE No Opera 52 Safari 10.1 WebView Android 65 Chrome Android 65 Firefox Android 61 Opera Android 47 Safari iOS 10.3 Samsung Internet Android 9.0
headers
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android 42 — 46 Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android 4.0 — 5.0
integrity
Chrome 46 Edge 14 Firefox Yes IE No Opera Yes Safari No WebView Android No Chrome Android 46 Firefox Android No Opera Android Yes Safari iOS No Samsung Internet Android 5.0
keepalive
Chrome 66 Edge 15 Firefox ? IE No Opera 43 Safari No WebView Android 66 Chrome Android 66 Firefox Android ? Opera Android 43 Safari iOS No Samsung Internet Android 9.0
method
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android 42 — 46 Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android 4.0 — 5.0
mode
Chrome 42 Edge 14 Firefox 39 IE No Opera 29 Safari No WebView Android 49 Chrome Android 49 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android 5.0
redirect
Chrome 46 Edge 14 Firefox Yes IE No Opera Yes Safari No WebView Android No Chrome Android 46 Firefox Android Yes Opera Android Yes Safari iOS No Samsung Internet Android 5.0
referrer
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 47 IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android No Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android No
referrerPolicy Chrome 52 Edge 14 Firefox 52 IE No Opera 39 Safari 11.1 WebView Android 52 Chrome Android 52 Firefox Android 52 Opera Android 41 Safari iOS No Samsung Internet Android 7.2
signal Chrome 66 Edge 16 Firefox Yes IE No Opera Yes Safari ? WebView Android 66 Chrome Android 66 Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android 9.0
url
Chrome 42
42
Fragment support added in Chrome 59.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
Fragment support added in Opera 46.
Safari 10.1 WebView Android 42
42
Fragment support added in Chrome 59.
Chrome Android 42
42
Fragment support added in Chrome 59.
Firefox Android No Opera Android 29
29
Fragment support added in Opera 46.
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
Fragment support added in Samsung Internet 7.0.

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅