Fetching XML from Cross Domain (CORS)

Thinking of fetching an information from cross-domain (CORS)?? An effective option is JSONP but it depends on the fact that the received information is in JSON format. Now, imagine a scenario where the information is received from third party using cross-domain call is in XML format. So, chances are you would get the response from that cross-domain but since the received format is not actually JSON, you can either get response in error() block with following reasons:

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

or

Uncaught SyntaxError: Unexpected token <

YQL Web Service can help you to tackle this scenario. You can use below method to make such cross-domain call and effectively fetch your XML.

//SampleURL is the concerned URL that returns xml
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + SampleURL + '"') + '&format=xml&callback=?';

// Request that YSQL string, and run a callback function.
$.getJSON(yql, function(data){
    console.log(data.results[0]);
});

I used this method to call Scribd API which actually return information as XML and the above method proved quite interestingly effective.

Enjoy!!

(Visited 71 times, 1 visits today)