Reputation: 13
I'm having some troubleS with the iTunes Search API. I can see the data when I access it directly via URL (i.e. http://itunes.apple.com/search?term=Reeder&country=fr&entity=software), but when I try it via a simple script, the data won't load.
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://itunes.apple.com/search?term=Reeder&country=fr&entity=software');
xhr.send();
xhr.addEventListener('readystatechange', function () {
if (xhr.readyState === xhr.DONE && xhr.status === 200) {
alert(xhr.responseText);
}
}, false);
Does anybody know what's going on?
Upvotes: 1
Views: 790
Reputation: 5131
As others said, your request violates same origin policies.
I had this problem a while ago and used YQL to create a JSONP callback (wraps the data in a javascript function). In other words, the script tag is not subject to same-origin policies
YQL: http://developer.yahoo.com/yql/ JSONP: http://en.wikipedia.org/wiki/JSONP (I'm assuming you know JSON)
You can learn the details yourself (how to use JSONP), so I'll just give you the example.
Your YQL query would be select * from json where url='http://itunes.apple.com/search?term=Reeder&country=fr&entity=software'
. If you change the type to JSON (in the console) you can specify a callback for JSONP. Console: http://developer.yahoo.com/yql/console
Link
You create a script tag and set the link/url above as the source. As you can see, the JSON data is wrapped with a function name. You should have defined the callback function, which will be called when the YQL/JSON source is loaded. That callback function then deals with the JSON data.
Upvotes: 2
Reputation: 76
Your request violates same origin policy.
In a few words you can't do an ajax call to a different domain. You can do it with an http request with a server-side language like php, java, nodejs or other.
Upvotes: 0
Reputation: 1145
That's a security feature of your browser... You can't access contents stored under an other domain using AJAX. Google for "Cross Domain AJAX Requests"
Upvotes: 0