XmlHtt prequest.onload不叫 [英] XmlHttpRequest.onload not called

查看:147
本文介绍了XmlHtt prequest.onload不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩弄这种 XmlHtt prequest 的事情。在一些教程和书籍,这是的onload 函数被调用时,请求完成的。在我的小实验,这个功能不会被调用。这是我的code:

I'm playing around with this XmlHttpRequest thing. In some tutorials and books, it is the onload function the one that is called when the request is done. In my little experiment, this function is never called. Here's my code:

    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);
    };

我在最后的Firefox版本(今天刚刚更新)测试这一点。在 onload事件日志行从不打印,我在第一行设置的断点永远不会命中。但是,的onreadystatechange 函数被调用两次,http请求实际上是由。这是Firebug的控制台显示:

I'm testing this on the last Firefox release (just updated today). The log line in onload is never printed, and the breakpoint I set in the first line is never hit. However, the onreadystatechange function is called twice, and the http request is actually made. This is what firebug's console shows:

    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);

有一个在发送行错误。我试图将其更改为 request.send()有相同的结果。

There's an error in the send line. I've tried changing it to request.send() with identical result.

起初我以为这可能是试图prevent XSS浏览器,让我感动我的html页面的驱动程序在我开发计算机一个Tomcat实例,但结果是一样的。

At first I thought this might be the browser trying to prevent XSS, so I moved my html driver page to a Tomcat instance in my dev machine, but the result is the same.

时此功能保证被调用?正如我上面所说的,这是常见的,在大多数教程,以待观察,但在另一方面,在的 W3C规范页面,世界您好段使用的onreadystatechange 来代替:

Is this function guaranteed to be called? As I've said above, it's common to be seen in most tutorials, but on the other hand in the W3C spec page, the hello world snippet uses onreadystatechange instead:

    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();

它检查的readyState == this.DONE 完成的值是4,这是我可以在我的日志中看到。所以,如果这是一个XSS相关的问题,而浏览器是preventing我,使连接到不同的域,那么为什么实际连接和DONE状态收到???

It checks readyState == this.DONE. DONE has the value 4, which is what I can see in my log. So if this were a XSS related issue, and the browser were preventing me to make the connection to a different domain, then why the actual connection is made and the DONE status is received???

PS:是的,我知道有强大的库来很容易地做到这一点,但我仍然是一个JavaScript的小白,所以我想先了解低电平

PS: Yes, I know there are powerful libraries to do this easily, but I'm still a JavaScript noob so I'd like to understand the low level first.

更新:
我已经改变了网址我的一个域(本地)内,并且该错误消失,但的onload 功能仍然没有被调用。经过测试,在IE8并不起作用。经过测试,在Chrome和作品。怎么样?

UPDATE:
I've changed the URL to one inside my domain (localhost), and the error is gone but the onload function is still not being called. Tested in IE8 and does not work. Tested in Chrome and works. How's that?

更新2:
在Firefox再次测试,而现在它的作品。也许是旧页面仍缓存所以这就是为什么我不能立刻注意到它。不过未能在IE8中,我会尝试在新版本进行测试。

UPDATE 2:
Tested again in Firefox, and now it works. Probably the old page was still cached so that's why I couldn't notice it immediatly. Still failing in IE8, I'll try to test it in a newer version.

推荐答案

看起来这的确是一个XSS问题和Firefox挡住了的onload 通话。我仍然不能明白为什么HTTP网络请求实际上正在做和的onreadystatechange 正在调用的完成 readyState的。

It looks like it was indeed a XSS issue and Firefox was blocking the onload call. I can't still understand why the http network request was actually being done and the onreadystatechange was being called with the DONE readyState.

我改变了URL到另一个在同一个领域,现在它在Firefox工程(一些高速缓存相关的错误尝试后),并在Chrome中。它仍然无法在IE8的工作,尽管的官方文档说,这是支持的。我发现这太回答其中另有规定的除外。它看起来像的onload 函数是一个更现代便捷的方法,并检查结果是用的老办法的onreadystatechange 而不是

I changed the URL to another one in the same domain, and now it works in Firefox (after some cache-related false attempts) and in Chrome. It still does not work in IE8, despite the official docs say it is supported. I've found this SO answer which states otherwise. It looks like the onload function is a more modern convenience method and the old way of checking the result is using onreadystatechange instead.

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

I guess I'll accept this answer as the solution unless a more detailed answer is provided.

这篇关于XmlHtt prequest.onload不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆