1

我正在尝试使用 AngularJS 查询Spotify 元数据 API,但我一直遇到以下错误。

Uncaught SyntaxError: Unexpected token : 

现在我通常知道在查询时您应该添加callback=JSON_CALLBACK为查询字符串,但在这种情况下它将不起作用。它返回:

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

我正在使用$http.jsonp().

没有回调的例子| 回调示例

那么,有没有办法使用纯Javascript解决这个问题,或者我最好添加一个服务器端包装器(我已经开始工作了,但如果它是纯Javascript的话)?

4

1 回答 1

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

于 2013-05-22T18:07:36.400 回答