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
event is fired, that is every time the
readyState
特性为
XMLHttpRequest
改变。
警告: This should not be used with synchronous requests and must not be used from native code.
XMLHttpRequest.onreadystatechange = callback;
callback
is the function to be executed when the
readyState
改变。
const xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method, url, true);
xhr.onreadystatechange = function () {
// In local files, status is 0 upon success in Mozilla Firefox
if(xhr.readyState === XMLHttpRequest.DONE) {
var status = xhr.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
console.log(xhr.responseText);
} else {
// Oh no! There has been an error with the request!
}
}
};
xhr.send();
| 规范 | 状态 | 注释 |
|---|---|---|
| XMLHttpRequest | 实时标准 | WHATWG (Web 超文本应用程序技术工作组) 实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
onreadystatechange
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5 | Opera 9 | Safari 1.2 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
完整支持