XMLHttpRequest: readystatechange イベント
        
        
          
                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月.
readystatechange イベントは、 XMLHttpRequest の readyState プロパティが変化するたびに発生します。
警告: これは同期リクエストで使用してはいけません。また、ネイティブコードから使用してはいけません。
構文
このイベント名を addEventListener() のようなメソッドで使用するか、イベントハンドラープロパティを設定するかしてください。
js
addEventListener("readystatechange", (event) => { })
onreadystatechange = (event) => { }
イベント型
一般的な Event であり、追加のプロパティはありません。
例
js
const xhr = new XMLHttpRequest();
const method = "GET";
const url = "https://developer.mozilla.org/";
xhr.open(method, url, true);
xhr.onreadystatechange = () => {
  // ローカルファイルでは、 Mozilla Firefox で成功するとステータスは0になります
  if (xhr.readyState === XMLHttpRequest.DONE) {
    const status = xhr.status;
    if (status === 0 || (status >= 200 && status < 400)) {
      // リクエストが正常に終了した
      console.log(xhr.responseText);
    } else {
      // あらら! リクエストでエラーが発生しました!
    }
  }
};
xhr.send();
仕様書
| Specification | 
|---|
| XMLHttpRequest> # event-xhr-readystatechange>  | 
            
| XMLHttpRequest> # handler-xhr-onreadystatechange>  | 
            
ブラウザーの互換性
Loading…