Forum Moderators: open

Message Too Old, No Replies

problem loading XML file using XMLHttpRequest

XML and XMLHttpRequest

         

dougcb68

6:47 pm on Nov 28, 2009 (gmt 0)

10+ Year Member



I'm a newbie trying to modify the code in an AJAX tutorial to load data from an XML file, and it is not working. The code is in a php file that is invoked with the URL:

getXMLTest.php?showName=photoShowTest.xml

The code I've written (mostly copied from the tutorial, actually) is:

<!Doctype HTML>
<html>

<head>

<script language="javascript" type="text/javascript">

var XMLDoc;


function getXMLDoc(XMLFile){

var XMLDocRequest=false;

try{
// Opera 8.0+, Firefox, Safari
XMLDocRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
XMLDocRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
XMLDocRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
XMLDocRequest.onreadystatechange = function(){
if(XMLDocRequest.readyState == 4){
alert("debug message 1:ready state=");
XMLDoc = XMLDocRequest.responseXML;
}
}

alert("debug message 2:XMLFile="+XMLFile);
XMLDocRequest.open("GET", XMLFile, false);
XMLDocRequest.setRequestHeader('Content-Type', "text/xml");
XMLDocRequest.send(null);
}

function initPhotoShow(fileName){
getXMLDoc(fileName);
alert("debug message 3:XMLDoc="+XMLDoc);
var photos = XMLDoc.getElementsByTagName('photo');
alert("debug message 4:");
//do clever things with the XML data ......
}

</script>
</head>

<body onload="initPhotoShow(&#34;<?php $inStr=$_GET['showName']; echo $showName; ?>&#34;)">
</body>

</html>

The contents of the XML file are:

<?xml version="1.0" encoding="ISO-8859-15"?>

<slides>
<photo name="sbMapPhotos/sanBasilio.jpg" height="600" />
<photo name="sbMapPhotos/touristOffice.jpg" height="435" />
<photo name="sbMapPhotos/touristDock.jpg" height="460" />
</slides>

The symptoms of my problem are:

Debug message 1 never executes, and when I try to get information on the ready state ie-

alert("debug message 1:ready state="+XMLDocRequest.readyState);

the entire program fails to run.

Debug message 2 executes properly and prints the proper file name.

Debug message 3 executes and prints "undefined" as the XMLDoc

Debug message 4 never executes.

I have spent the last three days searching the web for the undoubtedly stupid mistake I am making, without success. Thank you in advance for pointing out where I have gone astray.

Fotiman

2:38 pm on Nov 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First, I would start by making sure the server is actually returning the file. You can use a program like Wireshark or Fiddler to example the TCP packets that are coming back from the server, or you may be able to use Firebug to view the response. You may also want to prevent caching by including some dynamic data in the request. For example:

XMLFile += "&ck=" + (new Date()).getTime();
XMLDocRequest.open("GET", XMLFile, false);

dougcb68

4:09 am on Dec 6, 2009 (gmt 0)

10+ Year Member



It turns out that this code works fine in Internet Explorer and Google Chrome. It is a Firefox problem. After much butting my head into a stone wall for several more hours, I discovered that the problem was in the line:

XMLDocRequest.open("GET", XMLFile, false);

Changing the "false" to "true" (synchronous to asynchronous) makes the code work just fine in all three browsers. I have no idea why.