問題描述
我正在嘗試通過 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!