By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Trying to get xml data using AJAX function getDatafromXml in script

P: 2
I can get the text file data from Apache but not the xml file data. Both files are in the same place on C:\xampp\htdocs\mdata. The code for the xml data is this:




Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html> <html> <head> <title>Ajax Lesson</title> <script>
  2.         function getDataFromXml(){
  3.         var xmlhttp;
  4.         if(window.XMLHttpRequest){
  5.             xmlhttp = new XMLHttpRequest();
  6.  
  7.         } else{
  8.             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  9.  
  10.         }
  11.             xmlhttp.onreadystatechange=function(){
  12.             if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
  13.                 xmldata = xmlhttp.responseXML;
  14.                 var movies = xmldata.getElementsByTagName("movie");
  15.                 var output="";
  16.                 output += movies[0].getElementsByTagName('title')[0].firstChild.nodeValue;
  17.                 document.getElementById("data").innerHTML = output;
  18.             }
  19.         }
  20.         xmlhttp.open("GET","movies.xml", true);
  21.         xmlhttp.send();
  22.  
  23.         }
  24.  
  25.     </script> </head> <body> <div id="data"><h2>Replace this text</h2></div> <button type="button" onclick="getDataFromXml()">Get Data</button> </body> </html>
Jun 25 '14 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
the posted code is of the HTML page, not of the XML but that’s not really the point.

open your browser’s developer tools and check in the network section what response the AJAX call receives.
Jun 26 '14 #2

P: 2
The GET method has a status of 200! Guess that good!
Jul 14 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.