XMLHttpRequest.onreadystatechange

An EventHandler that is called whenever the readyState attribute changes. The callback is called from the user interface thread. The XMLHttpRequest.onreadystatechange property contains the event handler to be called when the readystatechange (en-US) event is fired, that is every time the readyState property of the XMLHttpRequest changes.

警告: This should not be used with synchronous requests and must not be used from native code.

The readystatechange event will not be fired when an XMLHttpRequest request is cancelled with the abort() method.

備註: UPDATE: it's firing in the latest version of browsers (Firefox 51.0.1, Opera 43.0.2442.991, Safari 10.0.3 (12602.4.8), Chrome 54.0.2840.71, Edge, IE11). Example here - just reaload page few times.

語法

XMLHttpRequest.onreadystatechange = callback;

  • callback is the function to be executed when the readyState changes.

範例

var xhr = new XMLHttpRequest(),
    method = "GET",
    url = "https://developer.mozilla.org/";

xhr.open(method, url, true);
xhr.onreadystatechange = function () {
        if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
            console.log(xhr.responseText);
        }
    };
xhr.send();

規範

Specification
XMLHttpRequest Standard
# event-xhr-readystatechange
XMLHttpRequest Standard
# handler-xhr-onreadystatechange

瀏覽器相容性

BCD tables only load in the browser