Skip to main content

Response

Response 类表示页面接收到的响应。


方法

🌐 Methods

allHeaders

Added in: v1.15 response.allHeaders

包含与此响应关联的所有响应 HTTP 标头的对象。

🌐 An object with all the response HTTP headers associated with this response.

用法

await response.allHeaders();

返回


body

Added before v1.9 response.body

返回带有响应正文的缓冲区。

🌐 Returns the buffer with response body.

用法

await response.body();

返回


finished

Added before v1.9 response.finished

等待此响应完成,总是返回 null

🌐 Waits for this response to finish, returns always null.

用法

await response.finished();

返回


frame

Added before v1.9 response.frame

返回发起此响应的Frame

🌐 Returns the Frame that initiated this response.

用法

response.frame();

返回


fromServiceWorker

Added in: v1.23 response.fromServiceWorker

指示此响应是否由服务工作者的 Fetch 处理程序处理(即通过 FetchEvent.respondWith)。

🌐 Indicates whether this Response was fulfilled by a Service Worker's Fetch Handler (i.e. via FetchEvent.respondWith).

用法

response.fromServiceWorker();

返回


headerValue

Added in: v1.15 response.headerValue

返回与名称匹配的头部的值。名称不区分大小写。如果多个头部具有相同的名称(set-cookie 除外),它们将作为列表返回,列表项由 , 分隔。对于 set-cookie,使用 \n 分隔符。如果未找到任何头部,则返回 null

🌐 Returns the value of the header matching the name. The name is case-insensitive. If multiple headers have the same name (except set-cookie), they are returned as a list separated by , . For set-cookie, the \n separator is used. If no headers are found, null is returned.

用法

await response.headerValue(name);

参数

返回


headerValues

Added in: v1.15 response.headerValues

返回所有与名称匹配的头部值,例如 set-cookie。名称不区分大小写。

🌐 Returns all values of the headers matching the name, for example set-cookie. The name is case-insensitive.

用法

await response.headerValues(name);

参数

返回


headers

Added before v1.9 response.headers

一个带有响应HTTP头的对象。头部名称为小写。注意,该方法不会返回安全相关的头部,包括与 Cookie 相关的头部。你可以使用 [response.allHeaders()](/api/class-response.mdx#response-all-headers) 来获取包含 'cookie' 信息的完整头部列表。

🌐 An object with the response HTTP headers. The header names are lower-cased. Note that this method does not return security-related headers, including cookie-related ones. You can use response.allHeaders() for complete list of headers that include cookie information.

用法

response.headers();

返回


headersArray

Added in: v1.15 response.headersArray

包含与此响应关联的所有请求 HTTP 头的数组。与 response.allHeaders() 不同,头名称不会被转换为小写。具有多个条目的头,例如 Set-Cookie,会在数组中出现多次。

🌐 An array with all the request HTTP headers associated with this response. Unlike response.allHeaders(), header names are NOT lower-cased. Headers with multiple entries, such as Set-Cookie, appear in the array multiple times.

用法

await response.headersArray();

返回


json

Added before v1.9 response.json

返回响应正文的 JSON 表示形式。

🌐 Returns the JSON representation of response body.

如果响应正文无法通过 JSON.parse 解析,该方法将抛出异常。

🌐 This method will throw if the response body is not parsable via JSON.parse.

用法

await response.json();

返回


ok

Added before v1.9 response.ok

包含一个布尔值,说明响应是否成功(状态范围为 200-299)。

🌐 Contains a boolean stating whether the response was successful (status in the range 200-299) or not.

用法

response.ok();

返回


request

Added before v1.9 response.request

返回匹配的 Request 对象。

🌐 Returns the matching Request object.

用法

response.request();

返回


securityDetails

Added in: v1.13 response.securityDetails

返回 SSL 和其他安全信息。

🌐 Returns SSL and other security information.

用法

await response.securityDetails();

返回

  • Promise<null | Object>#
    • issuer string (optional)

      证书中发行者字段的常用名称组件。此信息仅用于参考。可选。

    • protocol string (optional)

      使用的特定 TLS 协议。(例如 TLS 1.3)。可选。

    • subjectName string (optional)

      证书中主题字段的通用名称组件。此项仅供参考。可选。

    • validFrom number (optional)

      Unix 时间戳(以秒为单位),指定此证书何时生效。可选。

    • validTo number (optional)

      Unix 时间戳(以秒为单位),指定此证书何时失效。可选。


serverAddr

Added in: v1.13 response.serverAddr

返回服务器的 IP 地址和端口。

🌐 Returns the IP address and port of the server.

用法

await response.serverAddr();

返回


status

Added before v1.9 response.status

包含响应的状态代码(例如,200 表示成功)。

🌐 Contains the status code of the response (e.g., 200 for a success).

用法

response.status();

返回


statusText

Added before v1.9 response.statusText

包含响应的状态文本(例如,通常成功时为“OK”)。

🌐 Contains the status text of the response (e.g. usually an "OK" for a success).

用法

response.statusText();

返回


text

Added before v1.9 response.text

返回响应正文的文本表示形式。

🌐 Returns the text representation of response body.

用法

await response.text();

返回


url

Added before v1.9 response.url

包含响应的 URL。

🌐 Contains the URL of the response.

用法

response.url();

返回