开发者

How parse remote XML file with jQuery

I use this script to parse an XML file with jQuery, but it run only if I have the XML file in local server. Do you know how can I parse an XML file on remote server?

<script>$(document).ready(function(){
  $.ajax({
    type: "GET",
    url: "http://www.myotherwebsite.com/folder/myfile.xml",
    dataType: "xm开发者_开发技巧l",
    success: function(xml){
      $(xml).find("user").each(function(){
        var name = $(this).find("name").text();
        var email = $(this).find("email").text();
        var phone_number = $(this).find("mobile").text();

        document.write("<b>Name</b>: "+name+"<br>");
        document.write("<b>Email</b>: "+email+"<br>");
        document.write("<b>Phone Number</b>: "+phone_number+"<br>");
      })
    }
   });
});
</script>


Same Origin Policy would prevent remote access.


You can't access remote data with JavaScript (the browser) only.

You need some local server that does the remote access for you (proxy).

(local to the domain that JavaScript code is being executed on)


There are a few ways to get around this, but the typical approach to this problem is to make your requests to the other site from the server-side then return the results to the client.

So, in your case you can make an Ajax call to server-side code running on your local server. This code would then:

  • Request the xml file from the remote server
  • Return the results to your client-side code
  • You can then parse the xml as you are currently doing

The following article provides a guide for possible approaches to solving this problem, including the standard proxy approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜