javascript - unable to read xml file within project folder using AJAX request -
iam trying read simple xml file placed xml file in project root folder still when im accessing xml file says 404 not found dont know why if nay 1 know please me.
this content in newfile.xml
<?xml version="1.0" encoding="utf-8"?> <catalog> <cd> <title>empire burlesque</title> <artist>bob dylan</artist> <country>usa</country> <company>columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>hide heart</title> <artist>bonnie tyler</artist> <country>uk</country> <company>cbs records</company> <price>9.90</price> <year>1988</year> </cd> </catalog>
index.html
<!doctype html> <html> <head> <meta charset="iso-8859-1"> <title>insert title here</title> </head> <body> <button onclick="loadxml()">load xml document</button> <div id="display"></div> </body> <script type="text/javascript"> function loadxml(){ var xhttp = new xmlhttprequest(); xhttp.onreadystatechage = function(){ if(this.readystate==4 && this.status==200){ myfunction(this) } }; xhttp.open('get',"newfile.xml",true); xhttp.send(); } function myfunction(xm){ var i=0; var table = "<tr><th>artist</th><th>title</th></tr>"; var x = xm.responsexml; var cd = x.getelementsbytagname("cd"); (i = 0; i<= cd.length;i++){ table += "<tr><td>" + cd[i].getelementsbytagname("artist")[0].childnodes[0].nodevalue + "</td><td>" + cd[i].getelementsbytagname("title")[0].childnodes[0].nodevalue + "</td></tr>"; } document.getelementbyid("display").innerhtml = table; } </script> </html>
well if you're on *nix system "newfile.xml" not same "newfile.xml". if not i'd guess root of project not think is. in lot of frameworks root static files in subdirectory name such "public"
based on screenshot looks xml file in parent directory above index.html file is.
Comments
Post a Comment