Request() 构造函数创建新 Request 对象。

句法

var myRequest = new Request(input[, init]);
					

参数

input
Defines the resource that you wish to fetch. This can either be:
  • A USVString containing the direct URL of the resource you want to fetch.
  • A Request object, effectively creating a copy. Note the following behavioural updates to retain security while making the constructor less likely to throw exceptions:
    • If this object exists on another origin to the constructor call, the Request.referrer is stripped out.
    • If this object has a Request.mode of navigate mode value is converted to same-origin .
init 可选
An options object containing any custom settings that you want to apply to the request. The possible options are:
  • method : The request method, e.g., GET , POST . The default is GET .
  • headers : Any headers you want to add to your request, contained within a object or an object literal with ByteString 值。
  • body : Any body that you want to add to your request: this can be a Blob , BufferSource , FormData , URLSearchParams , USVString ,或 ReadableStream object. Note that a request using the GET or HEAD method cannot have a body.
  • mode : The mode you want to use for the request, e.g., cors , no-cors , same-origin ,或 navigate 。默认为 cors .
  • credentials : The request credentials you want to use for the request: omit , same-origin ,或 包括 。默认为 same-origin .
  • cache cache mode you want to use for the request.
  • redirect : The redirect mode to use: follow , error ,或 manual 。默认为 follow .
  • referrer : A USVString specifying no-referrer , client , or a URL. The default is about:client .
  • integrity : Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE= ).

错误

类型 描述
TypeError 由于 Firefox 43 , Request() will throw a TypeError if the URL has credentials, such as http://user:password@example.com.

范例

In our Fetch Request example (见 Fetch Request live ) we create a new Request object using the constructor, then fetch it using a GlobalFetch.fetch call. Since we are fetching an image, we run Body.blob on the response to give it the proper MIME type so it will be handled properly, then create an Object URL of it and display it in an <img> 元素。

var myImage = document.querySelector('img');
var myRequest = new Request('flowers.jpg');
fetch(myRequest).then(function(response) {
  return response.blob();
}).then(function(response) {
  var objectURL = URL.createObjectURL(response);
  myImage.src = objectURL;
});
					

In our Fetch Request with init example (见 Fetch Request init live ) we do the same thing except that we pass in an init object when we invoke fetch() :

var myImage = document.querySelector('img');
var myHeaders = new Headers();
myHeaders.append('Content-Type', 'image/jpeg');
var myInit = { method: 'GET',
               headers: myHeaders,
               mode: 'cors',
               cache: 'default' };
var myRequest = new Request('flowers.jpg',myInit);
fetch(myRequest).then(function(response) {
  ...
});
					

Note that you could also pass the init object into the fetch call to get the same effect, e.g.:

fetch(myRequest,myInit).then(function(response) {
  ...
});
					

You can also use an object literal as headers in init .

var myInit = { method: 'GET',
               headers: {
                   'Content-Type': 'image/jpeg'
               },
               mode: 'cors',
               cache: 'default' };
var myRequest = new Request('flowers.jpg', myInit);
					

You may also pass a Request 对象到 Request() constructor to create a copy of the Request (This is similar to calling the clone() method.)

var copy = new Request(myRequest);
						

注意 : This last usage is probably only useful in ServiceWorkers .

规范

规范 状态 注释
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 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.
cross-origin referrer stripped out and navigate mode converted to same-origin when constructor created from existing Request 对象。 Chrome Yes Edge 15 Firefox 54 IE No Opera Yes Safari 10.1 WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android No Safari iOS 10.3 Samsung Internet Android Yes
navigate mode Chrome 49 Edge 15 Firefox 46 IE No Opera Yes Safari 10.1 WebView Android No Chrome Android 49 Firefox Android Yes Opera Android No Safari iOS 10.3 Samsung Internet Android 5.0
发送 ReadableStream in request body Chrome No Edge No Firefox No IE No Opera ? Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
referrer init option Chrome ? Edge 15 Firefox 47 IE No Opera Yes Safari 10.1 WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android No Safari iOS 10.3 Samsung Internet Android Yes
Consume response body as a ReadableStream Chrome 43 Edge ≤79 Firefox 65
65
57 Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) 和 preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE No Opera ? Safari No WebView Android 43 Chrome Android 43 Firefox Android 65
65
57 Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) 和 preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android No Safari iOS 10.3 Samsung Internet Android 4.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