AL-zami
AL-zami

Reputation: 9066

can't fetch data from xml file

The following lines of ajax was written to fetch some data from an xml file.But it failed to do so.There is no error thrown.But no data is shown in the browser window.I have my xampp runnign but can't figure out why is this happening.Can anyone help me with this problem??

<body>
<p id='suggestion'></p>
<script>
   function initialize(){

      var suggest=document.getElementById('suggestion');
      var xmlhttp,txt,elem,l;
      if(window.XMLHttpRequest){
          xmlhttp=new XMLHttpRequest();
      }else if(window.ActiveXObject){
          xmlhttp=new ActiveXObject();
      }
      if(xmlhttp){

      if(xmlhttp.readyState==4 && xmlhttp.status==200){

      xmlhttp.onreadystatechange=function(){
           elem=xmlhttp.responseXML;
           l=elem.getElementsByTagName('cd');
           for(i=0;i<l.length;i++){
               txt+=l[i].getELementsByTagName('title')[0].firstChild.data;
           }

        }   
      xmlhttp.open('GET','new.xml',true);
      xmlhttp.send(null);

      suggest.innerHTML=txt;
      }
     }
   }
   window.onload=initialize;
</script>

xml file:

<? xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<catalog>
<cd>
   <title>Empire Burlesque</title>
   <artist>Bob Dylan</artist>
</cd>
<cd>
   <title>We are all we need</title>
   <artist>above and beyond</artist>
</cd>
</catalog>

Upvotes: 0

Views: 52

Answers (1)

mgrim
mgrim

Reputation: 1302

xmlhttp.onreadystatechange is assigned within a conditional wrapper - it won't be assigned unless xmlhttp was already in readyState 4 and had status 200 (and that's not likely).

Try swapping the lines

if(xmlhttp.readyState==4 && xmlhttp.status==200){

and

xmlhttp.onreadystatechange=function(){

Upvotes: 1

Related Questions