I am trying my hand at some AJAX for the first time, and am running into an error. I’ve seen some people with similar problems, but after reading through them I still can’t figure out what’s wrong with my code.
The problem I am getting is that rf.getElementsByTagName("motd")[0] is coming up as undefined.
Here’s the code:
function refresh() {
var url = document.getElementById("url");
var req;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange=function() {
if (req.readyState==4 && req.status==200) {
var rf;
if (window.DOMParser) {
parser = new DOMParser();
rf=parser.parseFromString(req,"text/xml");
} else {
rf = new ActiveXObject("Microsoft.XMLDOM");
rf.async = false;
rf.loadXML(req);
}
document.getElementById("result").innerHTML="<div id=\"success\">"+
rf.getElementsByTagName("motd")[0]+" ";
} else {
document.getElementById("result").innerHTML="<div id=\"working\">Checking...</div>";
}
}
req.open("POST","g2mc_status.php",true);
req.send("server="+url);
}
And the XML I am trying to process (which is all the .php file is set to output right now):
<minecraft server="mc.nrd.li" port="25565">
<motd>NerdConglomerate</motd>
<players>
<online>0</online>
<maximum>20</maximum>
</players>
</minecraft>
You are passing
reqinstead ofreq.responseTextorreq.responseXMLto the XML parser.Works fine otherwise: http://jsfiddle.net/cLGGZ/