SitePoint Sponsor

User Tag List

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

    xmlHTTP 'Undefined'?

    I've been working on a tutorial for AJAX and i keep getting this message and cannot locate the source problem. Anyone give me any pointers?

    'XMLHttp undefined Line 44'


    Thanks

    Code:
    function createXmlHttpRequestObject()
    {
    	var xmlHttp;
    
    	try 
    	{
    		xmlHttp = new XMLHttpRequest();
    	}
    	catch(e)
    	{
    	
    		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
    										"MSXML2.XMLHTTP.5.0",
    										"MSXML2.XMLHTTP.4.0",
    										"MSXML2.XMLHTTP.3.0",
    										"MSXML2.XMLHTTP",
    										"Microsoft.XMLHTTP");
    		
    		
    		for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++)
    		{
    			try
    			{
    		
    			xmlHttp =  new ActiveXObject(xmlHttpVersions[i]);
    			}
    			catch(e) {}
    		}
    	}
    		
    		
    		
    		if(!xmlHttp)
    			alert("Error Creating the XMLHttpRequest object.");
    		else
    			return xmlHttp;
    }
    
    
    function process()
    	{
    	
    		
    		if(xmlHttp)
    		{
    		
    		try
    				{
    					
    					
    					xmlHttp.open("GET", "phptest.php", true);
    					xmlHttp.onreadystatechange = handleRequestStateChange;
    					xmlHttp.send(null);
    				}
    			
    		
    				catch(e)
    				{
    					alert("Can't connect to the server:\n" + e.toString());
    				}
    		}	
    			
    	}	
    	
    function handleRequestStateChange()
    	{
    	
    	
    		if (xmlHttp.readyState == 4)
    		{
    			
    			if (xmlHttp.status == 200)
    			{
    				try
    				{
    				  handleServerResponse();
    				}
    				catch(e)
    				{
    				
    					alert("Error reading the response: " + e.toString());
    				}
    			}
    			else
    			{
    				
    				alert("There was a problem retreiving the data:\n" + xmlHttp.statusText);
    			}
    		}
    	}
    	
    function handleServerResponse()
    	{
    	
    		
    		var xmlResponse = xmlHttp.responseXML;
    		
    		//catching potential errors with IE and Opera
    		
    		if(!xmlResponse || !xmlResponse.documentElement)
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    		//catching potential errors with Firefox
    		var rootNodeName = xmlResponse.documentElement.nodeName;
    		if(rootNodeName == "parsererror")
    		throw("Invalid XML Structure");
    		
    		//obtain the XML documents element
    		xmlRoot = xmlResponse.documentElement;
    		
    		titleArray = xmlRoot.getElementsByTagName('title');
    		isbnArray = xmlRoot.getElementsByTagName('isbn');
    		
    		var html = "";
    		
    		for(var i=0; i<titleArray.length; i++)
    			html += titleArray.item(i).firstChild.data + ", " +
    			isbnArray.item(i).firstChild.data + "<br/>";
    			
    		
    			myDiv.innerHTML = html;
    	}
    PHP Code:
    <?php
    header
    ('Content-Type: text/xml');

    //create new xml document

    $dom = new DOMDocument();

    //create root element

    $response $dom->createElement('response');
    $dom->appendChild($response);

    //create book element

    $books $dom->createElement('books');
    $response->appendChild($books);

    //create title element for the book
    $title $dom->createElement('title');
    $titleText $dom->createTextNode('Building Responsive Web Applications with Ajax and PHP');
    $title->appendChild($titleText);

    //create the isbn element

    $isbn $dom->createElement('isbn');
    $isbnText $dom->createTextNode('1-904811-82-5');
    $isbn->appendChild($isbnText);

    //create the <book> element

    $book $dom->createElement('book');
    $book->appendChild($title);
    $book->appendChild($isbn);

    //append <book> as child of <books>
    $books->appendChild($book);

    //build xml structure in string variable

    $xmlString $dom->saveXML();
    //output xml string
    echo $xmlString;



    ?>

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Martin,

    Using 'var' to declare a variable in a function makes that variable 'local' to the function.
    Code:
    function createXmlHttpRequestObject()
    {
      var xmlHttp;
      ...
    it looks like you need it to be global
    Code:
    var xmlHttp = null;
    function createXmlHttpRequestObject()
    {
      ...

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah!

    Indeed i forgot to put the:

    var xmlHttp = createXmlHttpRequestObject();

    at the start of the document.


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
  •