0

I'm trying to use the iTunes search API with angularjs. This is me making my request

var itunesURL = 'https://itunes.apple.com/search?'
var encoded = encodeURIComponent($scope.search);
var apiURL = itunesURL + 'term=' + encoded + '&country=US&media=music&limit=5';

$http({
        method: 'GET',
        url: apiURL
    }).then(function successCallback(response) {
        console.log(response.data);
    }, function errorCallback(response) {
        console.log('the request failed');
    });

the search variable on the scope represents what the user typed in the search bar. But I'm getting this error...

XMLHttpRequest cannot load https://itunes.apple.com/search?term=What%20I%27ve%20Done&country=US&media=music&limit=5. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
Rockstar5645
  • 4,376
  • 8
  • 37
  • 62
  • The problem is in your error statement. You don't have access to the resource. `Access-Control-Allow-Origin` header is a server side header (i.e. it has to be set by Apple, Inc.) – War10ck Dec 21 '15 at 15:51
  • How would I get access to the resource? – Rockstar5645 Dec 21 '15 at 15:52
  • Possible duplicate of [Access-Control-Allow-Origin error sending a jQuery Post to Google API's](http://stackoverflow.com/questions/6114436/access-control-allow-origin-error-sending-a-jquery-post-to-google-apis) – War10ck Dec 21 '15 at 15:52
  • Try aliasing `localhost` in your `etc/hosts` file to something else, like this: `127.0.0.1 localhost something-else.com` – meatspace Dec 21 '15 at 15:53
  • 1
    It's detailed here, in the [official API docs](https://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html). You need to create dynamic script tags and provide a `&callback={function name}` parameter effectively making the call a `jsonp`. For more information on `jsonp` and to learn how it is used to access data across different domains, see [this article](https://en.wikipedia.org/wiki/JSONP). – War10ck Dec 21 '15 at 15:55
  • @meatspace Unfortunately, the message is referring to a security policy within the browser. Aliasing the `localhost` won't make it work in this case. It doesn't appear based on the docs that the api supports ajax calls. Just dynamic injected script tags... – War10ck Dec 21 '15 at 15:57

0 Answers0