Hey, I’ve been trying to get the grips with some basic ajax, but i’m having some problems. I can’t seem to get childNodes to ever work.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" language="javascript">
function makeRequest(url, e) {
elem = document.getElementById(e);
elem.innerHTML = "Loading...";
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = outContent;
http_request.open('GET', url, true);
http_request.send(null);
}
function outContent() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var xmldoc = http_request.responseXML;
var root_node = xmldoc.getElementsByTagName('root');
elem.innerHTML = root_node[0].childNodes[0].firstChild.text;
} else {
elem.innerHTML = 'There was a problem with the request.';
}
}
}
</script>
</head>
<body>
<span style="cursor: pointer; text-decoration: underline" onclick="makeRequest('test.xml', 'load')">
Make a request
</span>
<div id="load"></div>
</body>
</html>
This is the code I’m using, and it’s called this file:
<?xml version="1.0" ?>
<root>
<text>I'm a test.</text>
</root>
If I run the hasChildNodes() function it returns true, and I can get the tagName variables of the root tag, but whenever I throw a childNodes into it all I see is “Loading…” with no output of the data.