Body

Body mixin of the 抓取 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 Response . This provides these objects with an associated body (a stream ), a used flag (initially unset), and a MIME type (initially the empty byte sequence).

特性

Body.body 只读
A simple getter used to expose a ReadableStream of the body contents.
Body.bodyUsed 只读
A Boolean that indicates whether the body has been read.

方法

Body.arrayBuffer()
Takes a Response stream and reads it to completion. It returns a promise that resolves with an ArrayBuffer .
Body.blob()
Takes a Response stream and reads it to completion. It returns a promise that resolves with a Blob .
Body.formData()
Takes a Response stream and reads it to completion. It returns a promise that resolves with a FormData 对象。
Body.json()
Takes a Response stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text as JSON .
Body.text()
Takes a Response 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() ( Response implements body) to give the response its correct MIME type.

HTML Content

<img class="my-image" src="https://wikipedia.org/static/images/project-logos/frwiki-1.5x.png">
					

JS Content

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

规范

规范 状态 Comment
Fetch
The definition of 'Body' in that specification.
实时标准

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Body 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 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10 WebView Android 42 Chrome Android 42 Firefox Android ? Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS 10 Samsung Internet Android 4.0
arrayBuffer
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 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10 WebView Android No Chrome Android 42 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 Yes Samsung Internet Android No
blob
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 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10 WebView Android No Chrome Android 42 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 10 Samsung Internet Android No
body
Chrome 52 Edge ≤18 Firefox 65
65
57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE No Opera 39 Safari 10 WebView Android 52 Chrome Android 52 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 ) and the javascript.options.streams preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
Opera Android 41 Safari iOS Yes Samsung Internet Android 6.0
bodyUsed
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 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
formData
Chrome 60 Edge ≤79 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 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
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 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10 WebView Android No Chrome Android 42 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 10 Samsung Internet Android No
text
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 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10 WebView Android No Chrome Android 42 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 10 Samsung Internet Android No

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅