I'm delving into AJAX for the first time and encountering an error. I've seen similar issues posted by others, but none of the solutions I found seem to apply to my code.
The specific problem I'm facing is that
rf.getElementsByTagName("motd")[0]
is returning as undefined.
Below is the snippet of code causing trouble:
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);
}
Here's a sample of the XML content I am attempting to process (currently output by the .php file):
<minecraft server="mc.nrd.li" port="25565">
<motd>NerdConglomerate</motd>
<players>
<online>0</online>
<maximum>20</maximum>
</players>
</minecraft>