How to parse remote XML file using jQuery

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

<script>$(document).ready(function(){
  $.ajax({
    type: "GET",
    url: "http://www.myotherwebsite.com/folder/myfile.xml",
    dataType: "xml",
    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>
+3
source share
3 answers

A policy of the same origin will prevent remote access.

+8
source

You cannot access deleted data only using JavaScript (browser).

You need a local server that makes remote access (proxy) for you.

(locally for the domain in which the JavaScript code is running)

+3
source

, - , .

So, in your case, you can make an Ajax call to the server code running on your local server. Then this code will look like this:

  • Request xml file from remote server
  • Return the results to the client code
  • Then you can parse the xml as you are doing now

The following article provides guidance on possible approaches to solving this problem, including the standard proxy approach.

+2
source

Source: https://habr.com/ru/post/1753753/


All Articles