1

I'm trying to query the Spotify Metadata API with AngularJS but I keep running into the following error.

Uncaught SyntaxError: Unexpected token : 

Now I know typically when querying you should add callback=JSON_CALLBACK as a query string but in this case it won't work. It returns:

Failed to load resource: the server responded with a status of 400 (Bad Request)

I am using $http.jsonp().

Example without callback | Example with callback

So, is there a way around this using pure Javascript or I'm best add a server-side wrapper (which I've got working but rather if it was pure Javascript)?

WolfieZero
  • 377
  • 3
  • 16

1 Answers1

4

It doesn't seem like Spotify is providing jsonp support, but they do support CORS - so this should work:

function spotify_api($http) {
    var url = "http://ws.spotify.com/lookup/1/.json?uri=spotify:track:5PJSqY8jbYzr4a6dl5Ory1";
    //CORS support
    delete $http.defaults.headers.common['X-Requested-With'];

    $http.get(url).success(function(data) {
        console.log(data);
    });
}

See my update: http://jsfiddle.net/69kYH/

The bad news is that CORS doesn't seem to work properly in angular with older versions of IE - see AngularJS - Calling Flickr API fails with warning message

Community
  • 1
  • 1
joakimbl
  • 18,081
  • 5
  • 54
  • 53