SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My AJAX Application broke in Firefox 3

    Hey,

    I have a simple AJAX application that gets a XML document (generated by PHP) and parses it but for some reason the XMLHttpRequest object has got kaput in Firefox 3. It does not complain about not being able to get the file or anything but when you attempt to read the information it has suppose to have got via responseXML or responseText you just get "undefined".

    Anyone got any ideas?

    Works fine in IE6 (Don't have IE7 to test)

    Source:

    Code:
    var xmlhttp = createObj();
    var banner_img;
    var banner_url;
    var banner_caption;
     /* Create new XMLHttpRequest object */
    
    function createObj()
    {
    	var xmlhttp;
    	//works for all except IE6 and older
    	try
    	{
    		xmlhttp = new XMLHttpRequest();
    	}
    	catch(e)
    	{
    		var objVer = 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<objVer.length && !xmlhttp; i++)
    		{
    			try
    			{
    				xmlhttp = new ActiveXObject(objVer[i]);
    			}
    			catch (e) {}
    		}
    	}
    	if(!xmlhttp)
    	alert("Failed to create a valid XMLHttpRequest object. Please make sure your browser supports XMLHttpRequest or the XMLHTTP ActiveX Object");
    	else
    	return xmlhttp;
    }
     
    function getBanner()
    {
    	if(xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
    	{
    	alert("doing it");
    		xmlhttp.open("GET", "banner_gen.php", true);
    		xmlhttp.onreadystatechange = handleBanner;
    		xmlhttp.send(null);
    	} else {
    		setTimeout('getBanner', 1000);
    	}
    }
    
    function handleBanner()
    {
    	if(xmlhttp.readyState == 4)
    	{
    		if(xmlhttp.status == 200)
    		{
    			var xmlData;
    			var xmlElement;
    			
    			xmlData = xmlhttp.responseXML;
    			xmlElement = xmlData.documentElement;
    			
    			banner_img = xmlElement.childNodes[0].firstChild.text;
    			banner_url = xmlElement.childNodes[1].firstChild.text;
    			banner_caption = xmlElement.childNodes[2].firstChild.text;
    			
    			
    		}
    		else
    		{
    			alert("Failed to Connect to server using XMLHTTP: " + xmlhttp.statusText);
    		}
    	}
    }
    If it helps my PHP application sends the headers Content-Type: text/xml and provided well formed XML:

    <?xml version="1.0"?>
    <banner><img>test.gif</img><url>http://www.test.com</url><caption>Tesssttttt</caption></banner>

    Thanks,

    Tom

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When do you call getBanner? Do you see any errors in the Error Console?

  3. #3
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    When do you call getBanner? Do you see any errors in the Error Console?
    onLoad

    Error in error console:

    Error: xmlElement.childNodes[0].firstChild is null
    Source File: http://192.168.2.3/banner/banner_js.js
    Line: 72

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you get if you alert xmlElement.childNodes[0]?

    This works fine in Firefox 2 you said?

  5. #5
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I alert it I get:

    [object Text]

    And I have copied and pasted the code from an application I previous used back in the day when firefox 2 was out so I assume it works in Firefox 2.

    Quote Originally Posted by Pepejeria View Post
    What do you get if you alert xmlElement.childNodes[0]?

    This works fine in Firefox 2 you said?

  6. #6
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fixed, well not totally. On my PHP application that I used to generate the XML document ($dom is a instance of DOMDocument) when I commented out:

    $dom->formatOutput = true;

    It worked fine.

    For some reason firefox is getting confused by the tab PHP5 is putting into the XML document to make it look nice (xmlElement.childNodes[0].nodeValue outputted a tab). How can I fix this?

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I then assume that the XML you get has whitespaces. Carriage returns etc are counted as text nodes in Firefox. This will also fail in Firefox 2.

    Two solutions, either remove all the whitespaces from the XML output or update your JavaScript to handle white spaces. See the following link that explains this.


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
  •