Gregory
Gregory

Reputation: 569

Crossdomain request with YQL

I am trying use the Musicbrainz API through javascript. I found that we can now perform crossmain easily with YQL.

And I've done a jsfiddle file that does so: http://jsfiddle.net/HBCDF/1/

The problem I have with this snippet is that is doesn't work when the http response is XML. Does anyone has an idea how I could change the snippet and be able to get the XML result?

Greg

Upvotes: 2

Views: 1727

Answers (2)

Alex K
Alex K

Reputation: 7217

Add the format=json to the YQL url

var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=json&callback=?';

And use the if ( data.query.results != null ) check

http://jsfiddle.net/HBCDF/3/

Upvotes: 0

roto
roto

Reputation: 677

Cross domain requests must be done using jsonp as normal ajax requests don't support cross domain calls. The difference is that jsonp isn't making a true XmlHttpRequest instead it's adding this to your page:

<script type="text/javascript" src="TheCrossDomainUrl"></script>

If you're trying to pull XML down this way, it will fail since it's not valid javascript. I believe there are some ways to pull down the XML without making an XmlHttpRequest (see: http://ajaxian.com/archives/xml-messages-with-cross-domain-json).

Another option would be to add a server side page that brokers the request for you. Your current page would make a normal XmlHttpRequest to another page in your same domain, that page would make the call to Musicbrainz, Musicbrainz responds to your server side page which responds to the client side script.

Upvotes: 2

Related Questions