Back to Content

Response: text() method

files/en-us/web/api/response/text/index.md

latest2.7 KB
Original Source

{{APIRef("Fetch API")}}{{AvailableInWorkers}}

The text() method of the {{domxref("Response")}} interface takes a {{domxref("Response")}} stream and reads it to completion. It returns a promise that resolves with a {{jsxref("String")}}. The response is always decoded using UTF-8.

Syntax

js-nolint
text()

Parameters

None.

Return value

A Promise that resolves with a {{jsxref("String")}}.

Exceptions

  • AbortError {{domxref("DOMException")}}
  • {{jsxref("TypeError")}}
    • : Thrown for one of the following reasons:
      • The response body is disturbed or locked.
      • There was an error decoding the body content (for example, because the {{httpheader("Content-Encoding")}} header is incorrect).

Examples

In our fetch text example (run fetch text live), we have an {{htmlelement("article")}} element and three links (stored in the myLinks array.) First, we loop through all of these and give each one an onclick event handler so that the getData() function is run — with the link's data-page identifier passed to it as an argument — when one of the links is clicked.

When getData() is run, we create a new request using the {{domxref("Request.Request","Request()")}} constructor, then use it to fetch a specific .txt file. When the fetch is successful, we read a string out of the response using text(), then set the {{domxref("HTMLElement.innerText","innerText")}} of the {{htmlelement("article")}} element equal to the text object.

js
const myArticle = document.querySelector("article");
const myLinks = document.querySelectorAll("ul a");

for (const link of myLinks) {
  link.onclick = (e) => {
    e.preventDefault();
    const linkData = e.target.getAttribute("data-page");
    getData(linkData);
  };
}

function getData(pageId) {
  console.log(pageId);
  const myRequest = new Request(`${pageId}.txt`);
  fetch(myRequest)
    .then((response) => {
      if (!response.ok) {
        throw new Error(`HTTP error, status = ${response.status}`);
      }
      return response.text();
    })
    .then((text) => {
      myArticle.innerText = text;
    })
    .catch((error) => {
      myArticle.innerText = `Error: ${error.message}`;
    });
}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also