XMLHttpRequest: statusText 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

注意:此功能在 Web Workers 中可用,但 Service Workers 除外。

只讀的 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);

/**
 * Outputs the following:
 *
 * 0 UNSENT
 * 1 OPENED
 * 3 LOADING OK
 * 4 DONE OK
 */

規範

規範
XMLHttpRequest
# the-statustext-attribute

瀏覽器相容性

另見