javascript - Second request recives the first one that was still processing -
i have request system 2 unrelated functions making requests server. problem response not correct let me explain happening step step:
a background function makes request server server processes task 1 second unrelated background function makes request server client recieves response of task 1 second function recieves response first function. first function never gets response.
now don't know how solve it, know need distinguish them separately theres no conflictions here.
this current code handles request stuff:
function call_back(result,func){ if(typeof(func) != 'undefined' || func != false){ func(result); } else { return false; } } function caller(url,cfunc){ if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } else { xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=cfunc; xmlhttp.open("get",url,true); xmlhttp.send(); } function call_file(url,func){ //handles html files (not json_encoded) caller(url,function(){ if ( xmlhttp.readystate== 4 && xmlhttp.status== 200 ){ call_back(xmlhttp.responsetext,func); } }); } function call_data(url,func,jsonbool){ //handles json_encoded data caller(url,function(){ if (xmlhttp.readystate==4 && xmlhttp.status==200){ call_back(json.parse(xmlhttp.responsetext),func); } }); }
what can functions, preventing behaviour?
here example of how structure code - have used this, works, refined.
function ajax(url, callback,args){ var xhttp = init(); xhttp.onreadystatechange = process; function init() { if(window.xmlhttprequest) return new xmlhttprequest(); else if (window.activexobject) return new activexobject("microsoft.xmlhttp"); } function process() { if (xhttp.readystate==4 && xhttp.status==200) { if (callback) callback(xhttp.responsetext,args); else return xhttp.responsetext; } } this.get=function(){ xhttp.open("get", url, true); xhttp.send(null); } }
to use it:
var url = '/someurl'; var ajax = new ajax(url,yourcallback,parameters); ajax.get();
i believe drobinson talking more robust. should example started though.
Comments
Post a Comment