请求: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①

浏览器兼容性

另见