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
只读
default
,
reload
,
no-cache
).
Request.context
只读
audio
,
image
,
iframe
,等)
Request.credentials
只读
omit
,
same-origin
,
包括
). The default is
same-origin
.
Request.destination
只读
RequestDestination
enum describing the request's destination. This is a string indicating the type of content being requested.
Request.headers
只读
头
object of the request.
Request.integrity
只读
sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=
).
Request.method
只读
GET
,
POST
,等)
Request.mode
只读
cors
,
no-cors
,
same-origin
,
navigate
)。
Request.redirect
只读
follow
,
error
,或
manual
.
Request.referrer
只读
client
).
Request.referrerPolicy
只读
no-referrer
).
Request.url
只读
包含请求的 URL。
Request
实现
Body
, so it also inherits the following properties:
body
只读
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()
ArrayBuffer
representation of the request body.
Body.blob()
Blob
representation of the request body.
Body.formData()
FormData
representation of the request body.
Body.json()
JSON
representation of the request body.
Body.text()
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. |
实时标准 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Request
|
Chrome
42
|
Edge ≤18 |
Firefox
39
|
IE No | Opera 28 | Safari 10.1 | WebView Android 42 |
Chrome Android
42
|
Firefox Android
39
|
Opera Android 28 | Safari iOS 10.3 | Samsung Internet Android 4.0 |
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
|
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
|
Edge 14 |
Firefox
39
|
IE No |
Opera
29
|
Safari No | WebView Android No | Chrome Android No | Firefox Android No |
Opera Android
29
|
Safari iOS No | Samsung Internet Android No |
context
弃用
非标
|
Chrome 42 — 46 | Edge ? |
Firefox
39 — 42
|
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
|
Edge 14 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10.1 | WebView Android 42 | Chrome Android 42 | Firefox Android Yes |
Opera Android
29
|
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
|
Edge 14 |
Firefox
39
|
IE No |
Opera
29
|
Safari No | WebView Android No | Chrome Android 42 — 46 | Firefox Android No |
Opera Android
29
|
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
|
Edge 14 |
Firefox
39
|
IE No |
Opera
29
|
Safari No | WebView Android No | Chrome Android 42 — 46 | Firefox Android No |
Opera Android
29
|
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
|
Edge 14 | Firefox 47 | IE No |
Opera
29
|
Safari No | WebView Android No | Chrome Android No | Firefox Android No |
Opera Android
29
|
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
|
Edge 14 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10.1 |
WebView Android
42
|
Chrome Android
42
|
Firefox Android No |
Opera Android
29
|
Safari iOS 10.3 |
Samsung Internet Android
4.0
|
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
用户必须明确启用此特征。