问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/15569209/xmlhttprequest-not-firing-response-event-handler-for-ready-state-3-when-using-fl