SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem making an Asynchronous Call with XMLHttpRequest and XML

    I'm just working on a tutorial to get to grips with the core of AJAX. I seem to of come across a problem which i have located to the JavaScript file but can't seem to pinpoint. Any ideas?

    http://www.creativebubbles.co.uk/tut...s/xml/xml.html

    Code:
    //holds an instance of the XMLHttpRequest
    var xmlHttp = createXmlHttpRequestObject();
    
    
    //stores the reference of the object
    function createXmlHttpRequestObject()
    {
    	var xmlHttp;
    	//Should work for all browsers expect for IE6 and older
    	try 
    	{
    		xmlHttp = new XMLHttpRequest();
    	}
    	catch(e)
    	{
    		//assume IE6 or older
    		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
    										"MSXML2.XMLHTTP.5.0",
    										"MSXML2.XMLHTTP.4.0",
    										"MSXML2.XMLHTTP.3.0",
    										"MSXML2.XMLHTTP",
    										"Microsoft.XMLHTTP");
    		
    		//try every prog id until it works
    		for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++)
    		{
    			try
    			{
    			//try to create XMLHttpRequest Object
    			xmlHttp =  new ActiveXObject(xmlHttpVersions[i]);
    			}
    			catch(e) {}
    		}
    	}
    		
    		//return created object or return an error
    		
    		if(!xmlHttp)
    			alert("Error Creating the XMLHttpRequest object.");
    		else
    			return xmlHttp;
    	}
    	
    	//called to read the file from the server
    	function process()
    	{
    	
    		//only continue if xmlhttp isn't void
    		if(xmlHttp)
    		{
    		//try and connect to the server
    		try
    				{
    					//initate reading the xml file from server
    					
    					xmlHttp.open("GET", "books.xml", true);
    					xmlHttp.onreadystatechange = handleRequestStateChange;
    					xmlHttp.send(null);
    				}
    				//display error in case
    		
    				catch(e)
    				{
    					alert("Can't connect to the server:\n" + e.toString());
    				}
    		}	
    			
    	}
    		
    	//function to handle the response
    	function handleRequestStateChange()
    	{
    	
    		//display the status of the request
    		if (xmlHttp.readyState == 4)
    		{
    			//continue only if HTTP status is "oK"
    			if (xmlHttp.status == 200)
    			{
    				try
    				{
    				  handleServerResponse();
    				}
    				catch(e)
    				{
    					//display error message
    					alert("Error reading the response: " + e.toString());
    				}
    			}
    			else
    			{
    				//display status message
    				alert("There was a problem retreiving the data:\n" + xmlHttp.statusText);
    			}
    		}
    	}
    	
    	function handleServerResponse()
    	{
    		//read the message from the server
    		var xmlResponse = xmlHttp.responseXML;
    		//obtain the XML's document element
    		xmlRoot = xmlResponse.documentElement;
    		//obtain arrays with book titles and ISBNs
    		titleArray = xmlRoot.getElementsByTagName('title');
    		isbnArray = xmlRoot.getElementsByTagName('isbn');
    		//genrate the html output
    		var html = "";
    		//itrate through the array and create the html structure
    		for(var i=0; i<titleArray.length; i++)
    			html += titleArray.item(i).firstChild.data + ", " +
    			isbnArray.item(i).firstChild.data + "br/>";
    			
    			//display HTML
    			myDiv.innerHTML = "Server says: <br />" + html;
    	}

  2. #2
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Script is not at the server!

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't understand?

  4. #4
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your script doesn't do anything accept this:
    Code:
    var xmlHttp = createXmlHttpRequestObject();
    Ok, so now you created some object. But, if you don't use it to do something, then nothing will happen on your page.

    Also, doing this is any programming language is a big no-no:
    Code:
    var xmlHttp = createXmlHttpRequestObject();
    
    
    //stores the reference of the object
    function createXmlHttpRequestObject()
    {
    	var xmlHttp;
    You need to use a different name for every variable in your program. Technically your code will work, but it is very bad practice.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •