Jump to content

Problems with returning XML?


soad_x83

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/53204-problems-with-returning-xml/
Share on other sites

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

  • 9 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.