Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Response
  4. bodyUsed

Response: bodyUsed property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨March 2017⁩.

Note: This feature is available inWeb Workers.

ThebodyUsed read-only property of theResponse interface is a boolean value that indicates whether the body has been read yet.

Value

A boolean value.

Examples

CheckingbodyUsed

This example illustrates that reading the body of a response changes the value ofbodyUsed fromfalse totrue.

The example contains an empty image.

When the example's JavaScript runs, we fetch an image and assigns the returned promise to a variableresponsePromise.

When the user clicks "Use response", we check whether the response has been used already. If it has, we print a message. If it has not, we read the response body and used it to provide a value for the image'ssrc attribute.

HTML

html
<p><button>Use response</button> <button>Reset</button></p><p><img src="" width="150" /></p><pre></pre>

JavaScript

js
const useResponse = document.querySelector("#use");const reset = document.querySelector("#reset");const myImage = document.querySelector("#my-image");const log = document.querySelector("#log");const responsePromise = fetch(  "/shared-assets/images/examples/firefox-logo.svg",);useResponse.addEventListener("click", async () => {  const response = await responsePromise;  if (response.bodyUsed) {    log.textContent = "Body has already been used!";  } else {    const result = await response.blob();    const objectURL = URL.createObjectURL(result);    myImage.src = objectURL;  }});reset.addEventListener("click", () => {  document.location.reload();});

Result

Initially there is no value for the image. If you click "Use response" once, thenbodyUsed isfalse, so we read the response and set the image. If you then click "Use response" again, thenbodyUsed istrue, and we print the message.

Click "Reset" to reload the example, so you can try again.

Specifications

Specification
Fetch
# ref-for-dom-body-bodyused①

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp