Body
mixin
的
抓取 API
represents the body of the response/request, allowing you to declare what its content type is and how it should be handled.
Body
is implemented by both
Request
and
响应
. This provides these objects with an associated
body
(
stream
),
used flag
(initially unset), and a
MIME 类型
(initially the empty byte sequence).
Body.body
只读
ReadableStream
of the body contents.
Body.bodyUsed
只读
布尔
that indicates whether the body has been read.
Body.arrayBuffer()
响应
stream and reads it to completion. It returns a promise that resolves with an
ArrayBuffer
.
Body.blob()
响应
stream and reads it to completion. It returns a promise that resolves with a
Blob
.
Body.formData()
响应
stream and reads it to completion. It returns a promise that resolves with a
FormData
对象。
Body.json()
响应
stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text as
JSON
.
Body.text()
响应
stream and reads it to completion. It returns a promise that resolves with a
USVString
(text). The response is
always
decoded using UTF-8.
The example below uses a simple fetch call to grab an image and display it in an
<img>
tag. You'll notice that since we are requesting an image, we need to run
Body.blob()
(
响应
implements body) to give the response its correct MIME type.
<img class="my-image" src="https://wikipedia.org/static/images/project-logos/frwiki-1.5x.png">
const myImage = document.querySelector('.my-image');
fetch('https://upload.wikimedia.org/wikipedia/commons/7/77/Delete_key1.jpg')
.then(res => res.blob())
.then(res => {
const objectURL = URL.createObjectURL(res);
myImage.src = objectURL;
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
Fetch
The definition of 'Body' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Body
|
Chrome
42
|
Edge ≤18 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10 | WebView Android 42 | Chrome Android 42 | Firefox Android ? |
Opera Android
29
|
Safari iOS 10 | Samsung Internet Android 4.0 |
arrayBuffer
|
Chrome
42
|
Edge ≤18 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10 | WebView Android No | Chrome Android 42 | Firefox Android No |
Opera Android
29
|
Safari iOS Yes | Samsung Internet Android No |
blob
|
Chrome
42
|
Edge ≤18 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10 | WebView Android No | Chrome Android 42 | Firefox Android No |
Opera Android
29
|
Safari iOS 10 | Samsung Internet Android No |
body
|
Chrome 52 | Edge ≤18 |
Firefox
65
|
IE No | Opera 39 | Safari 10 | WebView Android 52 | Chrome Android 52 |
Firefox Android
65
|
Opera Android 41 | Safari iOS Yes | Samsung Internet Android 6.0 |
bodyUsed
|
Chrome
42
|
Edge ≤18 |
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 |
formData
|
Chrome 60 | Edge ≤79 |
Firefox
39
|
IE No | Opera 47 | Safari 10 | WebView Android 60 | Chrome Android 60 | Firefox Android No | Opera Android 44 | Safari iOS Yes | Samsung Internet Android 8.0 |
json
|
Chrome
42
|
Edge ≤18 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10 | WebView Android No | Chrome Android 42 | Firefox Android No |
Opera Android
29
|
Safari iOS 10 | Samsung Internet Android No |
text
|
Chrome
42
|
Edge ≤18 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10 | WebView Android No | Chrome Android 42 | Firefox Android No |
Opera Android
29
|
Safari iOS 10 | Samsung Internet Android No |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
用户必须明确启用此特征。