Trying to get xml data using AJAX function getDatafromXml in script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milatif19
    New Member
    • Jun 2014
    • 2

    #1

    Trying to get xml data using AJAX function getDatafromXml in script

    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:




    Code:
    <!DOCTYPE html> <html> <head> <title>Ajax Lesson</title> <script>
    		function getDataFromXml(){
    		var xmlhttp;
    		if(window.XMLHttpRequest){
    			xmlhttp = new XMLHttpRequest();
    		
    		} else{
    			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    		
    		}
    			xmlhttp.onreadystatechange=function(){
    			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
    				xmldata = xmlhttp.responseXML;
    				var movies = xmldata.getElementsByTagName("movie");
    				var output="";
    				output += movies[0].getElementsByTagName('title')[0].firstChild.nodeValue;
    				document.getElementById("data").innerHTML = output;
    			}
    		}
    		xmlhttp.open("GET","movies.xml", true);
    		xmlhttp.send();
    		
    		}
    	
    	</script> </head> <body> <div id="data"><h2>Replace this text</h2></div> <button type="button" onclick="getDataFromXml()">Get Data</button> </body> </html>
    Last edited by Rabbit; Jun 25 '14, 07:34 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • milatif19
      New Member
      • Jun 2014
      • 2

      #3
      The GET method has a status of 200! Guess that good!

      Comment

      Working...