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){
XMLDocRequest=false; // The variable that makes Ajax possible!
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("<?php $inStr=$_GET['showName']; echo $showName; ?>")">
</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.