此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

XMLHttpRequest:statusText 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

备注: 此特性在 Web Worker(不包括 Service Worker)中可用。

XMLHttpRequest.statusText 只读属性返回一个包含由 HTTP 服务器返回的响应状态消息的字符串。与表示数值状态码的 XMLHttpRequest.status 不同,此属性包含响应状态的文本,例如“OK”或“Not Found”。如果请求的 readyState 处于 UNSENTOPENED 状态,则 statusText 的值将为空字符串。

如果服务器响应未明确指定状态文本,statusText 将采用默认值“OK”。

备注:通过 HTTP/2 连接的响应其状态消息将始终为空字符串,因为 HTTP/2 不支持状态消息。

字符串。

示例

js
const xhr = new XMLHttpRequest();
console.log("0 UNSENT", xhr.statusText);

xhr.open("GET", "/server", true);
console.log("1 OPENED", xhr.statusText);

xhr.onprogress = () => {
  console.log("3 LOADING", xhr.statusText);
};

xhr.onload = () => {
  console.log("4 DONE", xhr.statusText);
};

xhr.send(null);

/**
 * 输出如下:
 *
 * 0 UNSENT
 * 1 OPENED
 * 3 LOADING OK
 * 4 DONE OK
 */

规范

Specification
XMLHttpRequest
# the-statustext-attribute

浏览器兼容性

参见