Cross-Domain AJAX XML Reader

Noobie is here. I am writing a client script that should read an XML file from another domain. I tried to use JSONP. I get a 200 response, but for some reason the client cannot access the returned data. I get two errors:

Resource interpreted as Script but transferred with MIME type text/xml 

and

 Uncaught SyntaxError: Unexpected token < 

Here's the code (I removed the XML URL as it is confidential):

 $(document).ready(function() { $.getJSON("urlOfFilecallback=?", function(data) { console.log(data) }) }); 

When I try to display data in the console, I get:

 ReferenceError: data is not defined 

How can i fix this? Do I need to use a proxy?

+6
source share
3 answers

You do not need to write your own proxy. You can use YQL if you want here, for example:

 //sample site that returns xml site = 'http://goo.gl/9iQWyG'; var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?'; // Request that YSQL string, and run a callback function. // Pass a defined function to prevent cache-busting. $.getJSON(yql, function(data){ console.log(data.results[0]); }); 

here is the jsfiddle check console.log.

( The limits of using the public YQL API are 2000 queries / hour on IP)

+15
source

XML is not allowed for cross-domain requests by default.

However, with a little server programming, you can create a proxy and load the data in your own domain and output it as XML.

for more information see Question

+1
source

If you have access to the other side of the domain, you can also use this approach . Cross-domain request

+1
source

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


All Articles