1. 給開發者的 Web 技術文件
  2. Web API
  3. XMLHttpRequest
  4. XMLHttpRequest:status 屬性

此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

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 2015年7月.

備註: 此功能可在 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

瀏覽器相容性

參見

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.

AltStyle によって変換されたページ (->オリジナル) /