javascript - how to read, parse and display an xml using jquery and ajax -
this question has answer here:
i trying read, parse , display xml file using jquery , ajax. while trying getting error due not able parse xml while
here code.
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script src="scripts/jquery-1.7.2.min.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> $(document).ready(function(){ $("#dvcontent").append("<ul></ul>"); $.ajax({ type: "get", url: "http://localhost/booklist.xml", datatype: "xml", success: function(xml){ $(xml).find('book').each(function(){ var stitle = $(this).find('title').text(); var spublisher = $(this).find('publisher').text(); $("<li></li>").html(stitle + ", " + spublisher).appendto("#dvcontent ul"); }); }, error: function() { alert("an error occurred while processing xml file."); } }); }); </script> <style type="text/css"> body { font-family : arial; font-size : 10pt; } </style> </head> <body> <form id="form1" runat="server"> <div id="dvcontent"> </div> </form>
and xml file given below
<?xml version="1.0" encoding="utf-8"?> <booklist> <book> <title>jquery: novice ninja</title> <publisher>site point</publisher> </book> <book> <title>learning jquery</title> <publisher>packt</publisher> </book> <book> <title>head first jquery</title> <publisher>o'reilly</publisher> </book> <book> <title>jquery ui 1.8</title> <publisher>packt</publisher> </book> </booklist>
the error getting
xmlhttprequest cannot load http://localhost/booklist.xml. no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access.
now dont know how add access-control-allow-origin xml file. if php have done here stuck.
the error because domain making request not match 1 receiving it. check url you're viewing site in , make sure domain matches, right down protocol , port number, http://localhost:8080
example.
failing that, can make request relative:
$.ajax({ type: "get", url: "/booklist.xml", // leading slash indicates url relative root // rest of code....
Comments
Post a Comment