XmlHttpRequest.onload未调用

Mis*_*ith 10 javascript ajax firefox xmlhttprequest

我正在玩这个XmlHttpRequest东西.在一些教程和书籍中,它是onload在请求完成时调用的函数.在我的小实验中,永远不会调用此函数.这是我的代码:

    window.onload = function() {
        var url = "http://www.google.com";
        var request = new XMLHttpRequest();


        request.onload = function() {
            var state = this.readyState;
            var responseCode = request.status;
            console.log("request.onload called. readyState: " + state + "; status: " + responseCode);

            if (state == this.DONE && responseCode == 200) {
                var responseData = this.responseText;
                alert("Success: " + responseData.length  + " chars received.");
            }
        };

        request.error = function(e) {
            console.log("request.error called. Error: " + e);
        };

        request.onreadystatechange = function(){
            console.log("request.onreadystatechange called. readyState: " + this.readyState);
        };

        request.open("GET", url);
        request.send(null);
    };
Run Code Online (Sandbox Code Playgroud)

我正在测试最后一个Firefox版本(今天刚刚更新).onload永远不会打印日志行,并且永远不会打到我在第一行中设置的断点.但是,该onreadystatechange函数被调用两次,并且实际发出了http请求.这就是firebug的控制台显示的内容:

    request.onreadystatechange called. readyState: 1
    GET http://www.google.com/    302 Found    174ms
    request.onreadystatechange called. readyState: 4
    NS_ERROR_FAILURE: Failure
        request.send(null);
Run Code Online (Sandbox Code Playgroud)

send行有一个错误.我已经尝试将其更改为request.send()相同的结果.

起初我以为这可能是浏览器试图阻止XSS,所以我将我的html驱动程序页面移动到我的开发机器中的Tomcat实例,但结果是一样的.

这个函数是否可以保证被调用?正如我上面所说,在大多数教程中都可以看到它,但另一方面在W3C规范页面中,hello world代码段使用onreadystatechange:

    function processData(data) {
      // taking care of data
    }

    function handler() {
      if(this.readyState == this.DONE) {
        if(this.status == 200 &&
           this.responseXML != null &&
           this.responseXML.getElementById('test').textContent) {
          // success!
          processData(this.responseXML.getElementById('test').textContent);
          return;
        }
        // something went wrong
        processData(null);
      }
    }

    var client = new XMLHttpRequest();
    client.onreadystatechange = handler;
    client.open("GET", "unicorn.xml");
    client.send();
Run Code Online (Sandbox Code Playgroud)

它检查readyState == this.DONE.DONE值为4,这是我在日志中可以看到的.因此,如果这是一个与XSS相关的问题,并且浏览器阻止我连接到另一个域,那么为什么要进行实际连接并收到DONE状态?

PS:是的,我知道有很强大的库可以很容易地做到这一点,但我仍然是一个JavaScript菜鸟,所以我想首先了解低级别.


更新:
我已将URL更改为我的域(localhost)中的一个,并且错误消失但该onload函数仍未被调用.在IE8中测试并不起作用.在Chrome中测试并且有效.怎么样?


更新2:
在Firefox中再次测试,现在它可以正常工作.可能旧的页面仍然被缓存,这就是为什么我不能立刻注意到它.在IE8中仍然失败,我将尝试在更新的版本中测试它.

Mis*_*ith 11

它看起来确实是一个XSS问题,Firefox阻止了这个onload电话.我仍然无法理解为什么http网络请求实际上已经完成并且onreadystatechange正在使用DONEreadyState 调用.

我将URL更改为同一域中的另一个URL,现在它可以在Firefox中运行(在一些与缓存相关的错误尝试之后)和Chrome中.它仍然无法在IE8中运行,尽管官方文档说它得到了支持.我发现了这个SO答案,否则说明了.看起来该onload函数是一种更现代的便捷方法,而旧的检查结果的方法是使用它onreadystatechange.

我想我会接受这个答案作为解决方案,除非提供更详细的答案.

  • 一段时间以来一直在寻找相同的问题.想知道是否有新的发现. (3认同)