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:

1

Resource interpreted asScript but transferred with MIME type text/xml

or

1

Uncaught SyntaxError:Unexpected token&lt;

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.

1

2

3

4

5

6

7

//SampleURL is the concerned URL that returns xml

varyql='http://query.yahooapis.com/v1/public/yql?q='+encodeURIComponent('select * from xml where url="'+SampleURL+'"')+'&amp;format=xml&amp;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.