Filip Trajkovski
Filip Trajkovski

Reputation: 53

Ajax response to xml page to get title elements doesn't work

Can anyone tell me why I can't get the elements from an XML document? It doesn't print anything when I press the et Title button that i have implemented in the body section. Here's my code:

function MyF () {
            var xmlhttp;
            var txt,x,i=0;
            var xx;
            if (window.XMLHttpRequest){
                    xmlhttp=new XMLHttpRequest();
            }
            else{
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.send();
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    x = xmlhttp.responseXML.documentElement.getElementsByTagName("CD");
                    xx = x[i].getElementsByTagName("TITLE");
                    document.getElementById("test").innerHTML=xx;
                }
            }
            xmlhttp.open("GET","cd_catalog.xml",true);
        }

Upvotes: 1

Views: 150

Answers (1)

Mouser
Mouser

Reputation: 13304

xmlhttp.responseXML.documentElement is the problem of your troubles. Just use xmlhttp.responseXML.getElementsByTagName and you should be fine.

Upvotes: 1

Related Questions