--- title: Response.bodyUsed slug: Web/API/Response/bodyUsed translation_of: Web/API/Response/bodyUsed tags: - API - Fetch - Property - Reference - bodyUsed - Response browser-compat: api.Response.bodyUsed ---
{{APIRef("Fetch")}}{{ SeeCompatTable }}
bodyUsed
是
{{domxref("Response")}} mixin中的一个只读属性。用以表示该body是否被使用过。
var myBodyUsed = response.bodyUsed;
{{domxref("Boolean")}}.
在以下fetch 请求示例(运行 fetch request live)。通过{{domxref("Request.Request")}}构造器创建了一个fetch请求,来获得一张JPG图片。当fetch成功后,通过{{domxref("Blob")}} 来使用了fetch返回的资源--{{domxref("URL.createObjectURL")}}创建该资源的URL,并作为 {{htmlelement("img")}}元素的src源来显示图片。
注意:在response.blob()被调用前后,
输出response.bodyUsed的差异。
<img class="my-image" src="https://wikipedia.org/static/images/project-logos/frwiki-1.5x.png">
var myImage = document.querySelector('.my-image'); fetch('https://upload.wikimedia.org/wikipedia/commons/7/77/Delete_key1.jpg').then(function(response) { console.log(response.bodyUsed); var res = response.blob(); console.log(response.bodyUsed); return res; }).then(function(response) { var objectURL = URL.createObjectURL(response); myImage.src = objectURL; });
{{ EmbedLiveSample('Example', '100%', '250px') }}
Specification | Status | Comment |
---|---|---|
{{SpecName('Fetch','#dom-body-bodyused','bodyUsed')}} | {{Spec2('Fetch')}} |
{{ CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | {{ CompatChrome(42) }} [1] |
{{CompatVersionUnknown}} | {{ CompatGeckoDesktop(39)}} [2] | {{ CompatNo }} |
29 [3] |
{{ CompatNo }} |
Feature | Android | Edge | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | {{ CompatNo }} | {{CompatVersionUnknown}} | {{ CompatNo }} | {{ CompatNo }} | {{ CompatNo }} | {{ CompatNo }} | {{ CompatNo }} | {{ CompatNo }} |
[1] Behind a preference in version 41.
[2] Behind a preference starting with version 34.
[3] Behind a preference in version 28.