0

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>
1
  • how is that related to php? Commented Mar 26, 2015 at 7:47

1 Answer 1

1

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(){
Sign up to request clarification or add additional context in comments.

2 Comments

ok i understand..i swapped them.but now firefox console is giving error 'not-well formed'
i solved the problem i mentioned in comment.There was a space between beginning arrow tag and 'xml' word at the beginning of my xml file.thanks anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.