Request()
构造函数创建新
Request
对象。
var myRequest = new Request(input[, init]);
USVString
containing the direct URL of the resource you want to fetch.
Request
object, effectively creating a copy. Note the following behavioural updates to retain security while making the constructor less likely to throw exceptions:
Request.referrer
is stripped out.
Request.mode
of
navigate
,
mode
value is converted to
same-origin
.
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. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Request()
构造函数
|
Chrome
41
|
Edge 15 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10.1 |
WebView Android
42
|
Chrome Android
41
|
Firefox Android
39
|
Opera Android
29
|
Safari iOS 10.3 |
Samsung Internet Android
4.0
|
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
|
IE No | Opera ? | Safari No | WebView Android 43 | Chrome Android 43 |
Firefox Android
65
|
Opera Android No | Safari iOS 10.3 | Samsung Internet Android 4.0 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。