Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript onreadystatechange回调是否应始终接收事件参数?_Javascript_Xmlhttprequest - Fatal编程技术网

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实例有

问题:

  • 标题是否与此差异有关

  • 是否有一种从回调参数或xml实例访问
    responseText
    属性的规范、防弹方法?添加检查这两个对象的逻辑是很容易的,但是我想知道在xmlhttpreq实例上是否需要设置一些选项