GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • MDN
  • HTML: Markup language
  • Elements
  • Global attributes
  • Attributes
  • See all…
  • Responsive images
  • HTML cheatsheet
  • Date & time formats
  • See all…
  • SVG
  • MathML
  • XML
  • CSS: Styling language

This view

  • Original page
  • New address

GeezerVison is showing developer.mozilla.org — Response: text() method - Web APIs | MDN

Response: text() method

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.

  • See full compatibility
  • Learn more

Note: This feature is available in Web Workers.

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

Syntax

js
text()

Parameters

None.

Return value

A Promise that resolves with a String.

Exceptions

AbortError DOMException

The request was aborted.

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 Content-Encoding header is incorrect).

Examples

In our fetch text example (run fetch text live), we have an <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 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 innerText of the <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

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

Browser compatibility

See also

  • ServiceWorker API
  • HTTP access control (CORS)
  • HTTP

Help improve MDN

Learn how to contribute

This page was last modified on Aug 20, 2025 by MDN contributors.

View this page on GitHub • Report a problem with this content
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.