XmlHtt prequest.responseText在加载(readyState的== 3)在Chrome [英] XmlHttpRequest.responseText while loading (readyState==3) in Chrome

查看:294
本文介绍了XmlHtt prequest.responseText在加载(readyState的== 3)在Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为流(从服务器到客户端)的Javascript阿贾克斯(由XmlHtt prequest(= XHR)。我正在使用说明修改用handleResponse功能 <一href="http://stackoverflow.com/questions/1112413/cross-browser-implementation-of-http-streaming-push-ajax-pattern">http://stackoverflow.com/questions/1112413/cross-browser-implementation-of-http-streaming-push-ajax-pattern

I am trying to "streaming" (from server to client) in Javascript by ajax (by XmlHttpRequest (=xhr). I am using modified handleResponse function described in http://stackoverflow.com/questions/1112413/cross-browser-implementation-of-http-streaming-push-ajax-pattern

function handleResponse() {
if (http.readyState != 4 && http.readyState != 3)
    return;
if (http.readyState == 3 && http.status != 200)
    return;
if (http.readyState == 4 && http.status != 200) {
    clearInterval(pollTimer);
    inProgress = false;
}
// In konqueror http.responseText is sometimes null here...
if (http.responseText === null)
    return;

while (prevDataLength != http.responseText.length) {
    if (http.readyState == 4  && prevDataLength == http.responseText.length)
        break;
    prevDataLength = http.responseText.length;
    var response = http.responseText.substring(nextLine);
    var lines = response.split('\n');
    nextLine = nextLine + response.lastIndexOf('\n') + 1;
    if (response[response.length-1] != '\n')
        lines.pop();

    for (var i = 0; i < lines.length; i++) {
        // ...
    }
}

if (http.readyState == 4 && prevDataLength == http.responseText.length)
    clearInterval(pollTimer);

inProgress = false;
}

使用PHP脚本,它刷新我的数据(没有Ajax真的刷新数据到浏览器,而进步)

With php script, which flushes me data (without ajax it really flushes data to browser while progressing)

我在Firefox没有问题,但谷歌Chrome和IE浏览器给我一个空responseText的同时xhr.readyState等于3。我发现在互联网上的问题描述,但它并没有给我任何的解决方案。

I have no problem in Firefox, but Google Chrome and IE give me an empty responseText while xhr.readyState equals to 3. I found that problem described in the Internet, but it didn't give me any solution.

你知道吗,如何通过在浏览器这个实现的问题? (W3C称,该responseText的不能为空在readyState的== 3 - 浏览器实现了这个规则,但只给出空字符串)

Do you know, how to pass by this implementation problem in Chrome? (w3c says, that responseText can't be NULL in readyState==3 - Chrome implemented this rule, but gives only empty string)

如果你不知道,你知道在某些产品中的任何可行的解决方案? (开源框架,librararies等。)

And if you don't know, do you know any working solution in some products? (opensource frameworks, librararies etc.)

非常感谢您的想法。

编辑: 解决方法是在创造的iframe,调用脚本通过的JavaScript IFRAME IFRAME和这里刷新数据和获取数据。但这不是Ajax解决方案。我真的希望看到纯粹的Ajax解决方案。

The workaround is in creating iframe, call the script to iframe and flush data here and grab data by javascript from iframe. But this is not ajax solution. I really would like to see pure ajax solution.

推荐答案

Chrome浏览器有一个错误在那里将只填充xhr.responseText已经获得了一定数量的字节后。有两种方法来解决这个问题,

Chrome has a bug where it will only populate xhr.responseText after a certain number of bytes has been received. There are 2 ways to get around this,

设置返回到应用程序/八位字节流

Set the content type of the return to "application/octet-stream"

发送约2KB一个prelude至$ P $页的处理程序。

Send a prelude of about 2kb to prep the handler.

这两种方法应该使铬填充responseText的领域时的readyState == 3。

Either of these methods should make chrome populate the responseText field when readyState == 3.

IE7 / 8,另一方面不能做到这一点,你需要求助于长轮询或使用跨域招用XDomainRequest在IE8,一拉<一href="http://blogs.msdn.com/b/ieinternals/archive/2010/04/06/comet-streaming-in-internet-explorer-with-xmlhtt$p$pquest-and-xdomainrequest.aspx">MS

IE7/8 on the other hand can't do it, you need to resort to long polling or use the cross domain trick with XDomainRequest in IE8, a la MS

这篇关于XmlHtt prequest.responseText在加载(readyState的== 3)在Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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