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
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

Link to comment
Share on other sites

  • 9 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.