XMLHttpRequest:status 屬性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

備註: 此功能可在 Web Worker(不包括 Service Worker)中使用。

XMLHttpRequest.status 唯讀屬性會回傳 XMLHttpRequest 回應的 HTTP 狀態碼數值。

在請求完成前,status 的值為 0。在發生 XMLHttpRequest 錯誤時,瀏覽器也會回報狀態為 0。

一個數字。

範例

js
const xhr = new XMLHttpRequest();
console.log("UNSENT:", xhr.status);

xhr.open("GET", "/server");
console.log("OPENED:", xhr.status);

xhr.onprogress = () => {
  console.log("LOADING:", xhr.status);
};

xhr.onload = () => {
  console.log("DONE:", xhr.status);
};

xhr.send();

/**
 * 輸出如下:
 *
 * UNSENT:0
 * OPENED:0
 * LOADING:200
 * DONE:200
 */

規範

Specification
XMLHttpRequest
# the-status-attribute

瀏覽器相容性

參見