soad_x83 Posted May 27, 2007 Share Posted May 27, 2007 Hi Guys, In dire need of some assistance Situation: My website makes many AJAX requests when I load a page, sometimes when I load a page everything is fine and at others some components refuse to load, this refussal to load mostly occurs after I clear the cache. The response function handles XML returned within a try statement, when a page doesn't load the catch output 'responseText' and alerts, but the response alert displays the correct XML response :S Prepare Function var ajaxRequest; ajaxRequest = prepare(); function prepare() { var ajaxRequest; try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } if(ajaxRequest) { return ajaxRequest; } } The request function function send(url, string) { addr = url; str = string; ajaxRequest; if(ajaxRequest) { try { // alert(ajaxRequest.readyState); if((ajaxRequest.readyState == 4 || ajaxRequest.readyState == 0)) { ajaxRequest.open("post", url, true); ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ajaxRequest.setRequestHeader("Cache-control", "no-cache"); ajaxRequest.send(str); ajaxRequest.onreadystatechange = response; } else { //alert("call" +addr +str); setTimeout("send(addr,str);",1000); } } catch(e) { setTimeout("send(addr,str)", 250); } } } The recieved code function response() { if(ajaxRequest.readyState == 4) { if(ajaxRequest.status == 200) { try { readResponse(); } catch(e) { error(e.toString()); } } else { setTimeout("send(addr,str)", 300); return false; //error(ajaxRequest.statusText); } } } function readResponse() { try { var response = ajaxRequest.responseXML; var xml = response.documentElement; var type = xml.getElementsByTagName("type")[0].firstChild.data; switch(type) { case 'Links' : displayLinks(xml); break; case 'userCheck' : displayCheck(xml); break; case 'List' : displayList(xml); break; case 'Select' : selectUnits(xml); break; ... } } catch(e) { var response = ajaxRequest.responseText; alert(response); [b] Where the valid XML gets thrown [/b] } If you can help I would be so grateful, Regards, Graham Quote Link to comment Share on other sites More sharing options...
448191 Posted May 27, 2007 Share Posted May 27, 2007 Are you sending the correct content-type header? I.e: header('Content-Type: text/xml'); Without this header, .responseXML will remain empty. Quote Link to comment Share on other sites More sharing options...
soad_x83 Posted May 27, 2007 Author Share Posted May 27, 2007 Thanks for the reply, Yeah, I'm thinking the problem may be due to multiple requests going on at once, It's quite weird, it's like the references to other external javascript (non-ajax) cause the thing thing to fall-over. i.e If I send one or many requests the result is the same intermittent sucsess Quote Link to comment Share on other sites More sharing options...
POGRAN Posted March 23, 2008 Share Posted March 23, 2008 maybe you should change: var type = xml.getElementsByTagName("type")[0].firstChild.data; to var type = xml.getElementsByTagName("type")(0).firstChild.data; Quote Link to comment Share on other sites More sharing options...
POGRAN Posted March 23, 2008 Share Posted March 23, 2008 + function name "send" is not the best option I recomment to use something more special (eg. sending_process, send_data, refresh_div...) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.