ajax: returns whole html page instead of xml content only

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

ajax: returns whole html page instead of xml content only

Post by raghavan20 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i have a 'link management' system. this allows to store links. i have an option to generate xml file for all the links available. it generates a xml of format.

Code: Select all

<Links>
<Link>
..
..
</Link>
...
...
</Links>
but for some reason when i use ajax, i get a lot of current page html content returned in http responseText. Could any of you please tell me why this is happening?

javascript code:

Code: Select all


core.js

//global var: will be used by many functions
var http = createRequestObject();

//create xmlHTTP obj
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}



function sndReq(sendMethod, url, event) {
	//alert( 'sendMethod: ' + sendMethod + ' url: ' + url + ' event: ' + event );
    http.open( sendMethod, 'http://' + url, true);
    http.onreadystatechange = function(){
      //alert( 'http state: ' + http.readyState + 'event: ' + event );
	  if(http.readyState==4){
	    	// Get the data from the server's response
	    	if( event == 'generateXML' ){
	    		//alert( 'generateXML event matched' );
	    		generateXMLForLinks( http.responseText );
	    	}
	    }
	  }
    http.send(null);
    
}


links1.js
	function generateXMLForLinks( input ){
		
		//var sendMethod = 'get';
		//var url = 'thisisraghavan.com/misc/links.php?action=generateXML';
		//var input;
		//input = sndReq( sendMethod, url );
		
		//alert( 'generateXMLForLinks called' + 'input: ' + input );
		var output = '<textarea cols="80" rows="5">' + input + '</textarea>';
		document.getElementById( 'output' ).innerHTML = output;
	}
	



call line: 

			<a href = 'javascript:sndReq("get", "thisisraghavan.com/misc/links.php?action=generateXML", "generateXML")'>Generate XML for Links</a>


for a demo, please try the link 'generate xml for links' from http://thisisraghavan.com/misc/links.php and see the html content appended after xml output in the textarea box dynamically generated.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

http://thisisraghavan.com/misc/links.ph ... enerateXML

Check that page, it's not returning strictly XML data.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Your AJAX is OK.
I tried to go here - http://thisisraghavan.com/misc/links.ph ... enerateXML with my browser and got the same result (XML block + HTML page). Check your script.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

oh, thanks for pointing that out. i fixed it now.

i was converting a normal php controller page to work with ajax. in the way, i forgot to use a exit() which resulted in printing out rest of the html in the php page. thanks to both of you guys.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Use

Code: Select all

http.responseXML()
Check this tutorial http://www.w3schools.com/php/php_ajax_xml.asp is similar with what you want to do.
Post Reply