Javascript onreadystatechange回调是否应始终接收事件参数?
在他们的示例中,Javascript onreadystatechange回调是否应始终接收事件参数?,javascript,xmlhttprequest,Javascript,Xmlhttprequest,在他们的示例中,onreadystatechange回调没有收到任何参数 var xhr = new XMLHttpRequest(), method = "GET", url = "https://developer.mozilla.org/"; xhr.open(method, url, true); // no param! xhr.onreadystatechange = function () { if(xhr.readyState === XMLHt
onreadystatechange
回调没有收到任何参数
var xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method, url, true);
// no param!
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// the response would be found on the xhr instance
console.log(xhr.responseText);
}
};
xhr.send();
但是,我看到响应将在传递给回调的事件参数中找到
xmlHttpRequest.onreadystatechange = function (event) {
var xhr = event.target;
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("target").innerHTML = xhr.responseText
}
};
我遇到了一些边缘情况,其中父对象确实很重要。在某些情况下,onreadystatechange
回调参数没有responseText
属性,但xml实例有
问题:
responseText
属性的规范、防弹方法?添加检查这两个对象的逻辑是很容易的,但是我想知道在xmlhttpreq实例上是否需要设置一些选项