Hi, I'm fairly new to AJAX, but I've been able to retrieve HTML and
plain-text documents without any trouble. However, I haven't figured
out how to retrieve it in XML format. Basically, here's the script
that's supposed to retrieve and parse the data when you pass it the url
of the page generating the XML data:
<script type="text/javascript">
var xmlHttp
function getresponse(url )
{
xmlHttp=GetXmlH ttpObject()
xmlHttp.onready statechange=sta teChanged;
xmlHttp.open("G ET",url,true) ;
xmlHttp.send(nu ll);
}
function stateChanged()
{
if (xmlHttp.readyS tate==4)
{
getById('firstn ame').value=xml Http.responseXM L.getElementsBy TagName("firstn ame")[0].childNodes[0].nodeValue;
getById('lastna me').value=xmlH ttp.responseXML .getElementsByT agName("lastnam e")[0].childNodes[0].nodeValue;
}
}
function GetXmlHttpObjec t()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest( );
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
}
catch (e)
{
xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
}
}
return xmlHttp;
}
</script>
Note that getById is just a function I made up to save space... the
problem is not with actually placing the content on the page I don't
think, so don't worry about it.
Here's the php file that generates the XML response:
<?php
(some database query returning a first and last name)
echo '<?xml version="1.0" ?>';
echo '<firstname>';e cho $record->firstname;ec ho '</firstname>';
echo '<lastname>';ec ho $record->lastname;ech o '</lastname>';
?>
I think the problem is either that (a) I'm not parsing the returned XML
file correctly, or (b) the file is not actually being returned in XML
format. Any ideas?