php - Ajax Request in Progress suddenly started returning status = 0 -
i have web project in php , accesses java project uses restlet framework. web project running on apache , testing using localhost. restlet framework uses localhost domain, url different: localhost:8888/
this javascript that, using ajax, makes call 1 of java classes (collectionpublic) using url above.
var url = "<?php echo $config['restserver_url'] ?>collectionpublic"; var params= "pagelist="+facebookpages+"&time="+time; var client = new xmlhttprequest(); client.open("post", url,true); client.setrequestheader("content-type", "application/x-www-form-urlencoded"); client.onreadystatechange = function () { if (client.readystate != 4) return; if (client.status != 200 && client.status != 304) { alert("error "+client.status); } else { alert("success"); } callback(client); } if (client.readystate == 4) return; client.send(params);
i have tested , call being made correctly, using url localhost:8888/collectionpublic, , reaching collectionpublic class (the class working fine).
the problem is: when call made, collectionpublic class takes long time complete task, , user should able access other pages (on same server) or reload page. however, when either of these things happen, alert("error "+client.status) pops , value of client.status 0. call aborted, collectionpublic's task continue normally, , when finishes, nothing happens in web page (before, alert("success") being fired).
i spent hours trying figure out causing error, since working last week. of posts found said cross-origin resource problem, since localhost , localhost:8888 not considered same domain. see if problem, started chrome using --disable-web-security argument (and disabled) issue still there. weirdest thing has worked before, , changed absolutely nothing in code.
i have seen post reloading page while ajax request in progress gives empty response , status zero , seems quite similar facing.
hopefully, have made myself clear, if have doubts regarding issue, ask.
thanks lot in advance.
i'm not convinced ajax request quite right. if (client.readystate != 4) return;
true aside when 4. may better:
client.onreadystatechange = function () { if(client.readystate < 4) { //not complete yet return; } if(client.status != 200 && client.status != 304) { //an error alert("error "+client.status); return; } if(client.readystate === 4) { //complete callback(client); } }
as problem whereby ajax call aborted: correct behaviour. xhr calls aborted browser page reloaded or unloaded. perhaps somehow not case when viewing pages locally. not allow user navigate away (or reload) whilst ajax in progress. work-around, class set session variable read page.
Comments
Post a Comment