XmlHttpRequest.responseText while loading (readyState==3) in Chrome(在 Chrome 中加载 (readyState==3) 时的 XmlHttpRequest.responseText)
问题描述
我正在尝试通过 ajax(通过 XmlHttpRequest (=xhr) 在 Javascript 中流式传输"(从服务器到客户端).我正在使用中描述的修改后的 handleResponse 函数HTTP Streaming"的跨浏览器实现(推)AJAX 模式
I am trying to "streaming" (from server to client) in Javascript by ajax (by XmlHttpRequest (=xhr). I am using modified handleResponse function described in 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('
');
nextLine = nextLine + response.lastIndexOf('
') + 1;
if (response[response.length-1] != '
')
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 中没有问题,但是 Google 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.
你知道,如何绕过Chrome中的这个实现问题吗?(w3c 说,在 readyState==3 中 responseText 不能为 NULL - Chrome 实现了这个规则,但只给出了空字符串)
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)
如果您不知道,您知道某些产品中有什么可行的解决方案吗?(开源框架、库等)
And if you don't know, do you know any working solution in some products? (opensource frameworks, librararies etc.)
非常感谢您的想法.
解决方法是创建 iframe,将脚本调用到 iframe 并在此处刷新数据,然后通过 javascript 从 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,
设置返回的内容类型为application/octet-stream"
Set the content type of the return to "application/octet-stream"
或
发送一个大约 2kb 的前奏来准备处理程序.
Send a prelude of about 2kb to prep the handler.
当 readyState == 3 时,这些方法中的任何一个都应该让 chrome 填充 responseText 字段.
Either of these methods should make chrome populate the responseText field when readyState == 3.
另一方面,IE7/8 做不到,你需要借助长轮询或在 IE8 中使用 XDomainRequest 的跨域技巧,例如 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
这篇关于在 Chrome 中加载 (readyState==3) 时的 XmlHttpRequest.responseText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Chrome 中加载 (readyState==3) 时的 XmlHttpRequest.responseText
基础教程推荐
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01