D
danep
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=GetXmlHttpObject()
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
getById('firstname').value=xmlHttp.responseXML.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
getById('lastname').value=xmlHttp.responseXML.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
}
}
function GetXmlHttpObject()
{
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.XMLHTTP");
}
}
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>';echo $record->firstname;echo '</firstname>';
echo '<lastname>';echo $record->lastname;echo '</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?
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=GetXmlHttpObject()
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
getById('firstname').value=xmlHttp.responseXML.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
getById('lastname').value=xmlHttp.responseXML.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
}
}
function GetXmlHttpObject()
{
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.XMLHTTP");
}
}
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>';echo $record->firstname;echo '</firstname>';
echo '<lastname>';echo $record->lastname;echo '</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?