Request 接口在 抓取 API 表示资源请求。

You can create a new Request 对象使用 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 ,等)
Request.credentials 只读
Contains the credentials of the request (e.g., omit , same-origin , 包括 ). 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 object of the request.
Request.integrity 只读
包含 subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE= ).
Request.method 只读
包含请求方法 ( GET , POST ,等)
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 只读

包含请求的 URL。

Request 实现 Body , so it also inherits the following properties:

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

方法

Request.clone()
创建副本为当前 Request 对象。

Request 实现 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.

注意 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);
  });
					

规范

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

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
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 默认为 包括 (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 默认为 包括 (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 默认为 包括 (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.

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. 抓取 API
  2. Request
  3. 构造函数
    1. Request()
  4. 特性
    1. body
    2. bodyUsed
    3. cache
    4. context
    5. credentials
    6. destination
    7. headers
    8. integrity
    9. method
    10. mode
    11. redirect
    12. referrer
    13. referrerPolicy
    14. url
  5. 方法
    1. arrayBuffer()
    2. blob()
    3. clone()
    4. formData()
    5. json()
    6. text()
  6. Related pages for Fetch API
    1. Body
    2. 响应
    3. WindowOrWorkerGlobalScope.fetch()

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1