請求:text() 方法

Baseline 已廣泛支援

此特性已得到良好確立,可跨多種裝置和瀏覽器版本使用。自 2017 年 3 月起,所有瀏覽器均支援此特性。

注意:此功能在 Web Workers 中可用。

Request 介面的 text() 方法讀取請求體並將其作為 Promise 返回,該 Promise 解析為 String。響應始終使用 UTF-8 進行解碼。

語法

js
text()

引數

無。

返回值

一個解析為 String 的 Promise。

示例

js
const text = "Hello world";

const request = new Request("/myEndpoint", {
  method: "POST",
  body: text,
});

request.text().then((text) => {
  // do something with the text sent in the request
});

規範

規範
Fetch
# ref-for-dom-body-text①

瀏覽器相容性

另見