当使用flush from php时,xmlhttprequest不触发就绪状态3的响应事件处理程序 [英] xmlhttprequest not firing response event handler for ready state 3 when using flush from php
问题描述
我使用xmlhttprequest从Google Chrome扩展程序发送请求远程服务器
我在manifest.json中设置了访问远程主机的权限
基本上它工作正常如预期。我想要的是readystate 4当响应完成时触发。
因为它是一个8到10秒的过程在服务器端我使用echo从服务器
因此,我使用readyState == 3条件显示服务器响应
但是当我测试的readystate 1和4正在触发事件处理程序而不是2和3
这里是我使用的代码
var wini = window.open('','');
var sta ='';
var jax = new XMLHttpRequest();
jax.onreadystatechange = function(){
sta = sta + jax.readyState +','
wini.document.write(sta +'< br>');
}
jax.open(POST,http://sitename.com/subscript/?save=save&tstamp=+ Math.random());
jax.setRequestHeader(Content-Type,application / x-www-form-urlencoded);
//jax.setRequestHeader(\"Connection:close);
jax.send(somedata =+ encodeURIComponent(window.somedata));
不同的结果。除了readystate 1没有其他被触发。
因此,readystates 1和4工作正常,但不是2和3.最需要的是3。
代码运行实际发生的是:
1,打印在弹出窗口然后8到9秒通过,当就绪状态为4时
1,2
< 3
1,2,3,4
上述三行都是一次列印。所以我假设只有readystate 4我从服务器得到所有的响应,而不是在阅读状态为3时。
我在其他Web应用程序中使用了readystate 3
这个xhr我不知道我失踪了什么。
如何使用readystate 3这里?
最后我发现只有在chrome和safari它不因为chrome正在使用safari webkit,可能是webkit不想像别人一样工作。
添加了内容类型标题, strong>
另一个用户有类似的问题,我一直在试验,并发现了以下解决方案。
这里是我的回答。
I am sending a request a remote server from google chrom extension using xmlhttprequest
I have set permissions in the manifest.json to access remote hosts
Basically it is working fine as expected. what i had expected is readystate 4 it is fired when the response has completed.
Since it is a 8 to 10 second process in the server side i use echo from the server side to the client for status update.
so i use readyState==3 condition to show server response
but when i tested readystate 1 and 4 are firing the event handler and not 2 and 3
here is the code i use
var wini = window.open('','');
var sta='';
var jax = new XMLHttpRequest();
jax.onreadystatechange = function() {
sta = sta + jax.readyState + ', '
wini.document.write(sta+'<br>');
}
jax.open("POST","http://sitename.com/subscript/?save=save&tstamp="+Math.random());
jax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//jax.setRequestHeader("Connection: close");
jax.send("somedata=" + encodeURIComponent(window.somedata));
using connection close. a different result. except readystate 1 no others were fired
So, readystates 1 and 4 are working fine but not 2 and 3. what is most needed is 3.
what is actually happens with the code runs is:
1, is printed in the popup window and then 8 to 9 secs pass and when ready state is 4
1,2
1,2,3
1,2,3,4
the above three lines are printed in one shot. so i assume only by readystate 4 i am getting all the response from the server and not while readystate is 3.
I have used readystate 3 in other web applications which is of same origin and it worked and is still working.
for this xhr i don't know what i am missing.
How to make use of readystate 3 here?
at last i found that only in chrome and safari it does not work because chrome is using safari webkit and may be webkit doesn't want to work like others.
added a content type header and it worked as expected
another user had a similar issue and i had been experimenting and found the following solution.
since my solutions solves both the questions i am linking the answer which i had already given in others question instead of duplicating it here.
here is my answer.
PHP Flush() not working in Chrome
这篇关于当使用flush from php时,xmlhttprequest不触发就绪状态3的响应事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!