2012-12-19 93 views
1

我只是想使用angularjs http進行itunes搜索api的基本調用。AngularJs和itunes

$http.jsonp("http://itunes.apple.com/search", {params:{"term":"jack+johnson"}}) 
       .success(function (data, status, headers, config) { 
        console.log("is scope : " + data); 
       }).error(function (data, status, headers, config) { 
        console.log("error" + status); 
       }); 

的請求是做得很好(狀態:200),我可以看到JSON檢索,這是一個摘錄:

{ 
"resultCount":50, 
"results": [ 
{"wrapperType":"track", "kind":"song", "artistId":909253, "collectionId":120954021,     "trackId":120954025, "artistName":"Jack Johnson", "collectionName":"Sing-a-Longs and Lullabies  for the Film Curious George", "trackName":"Upside Down", "collectionCensoredName":"Sing-a- Longs and Lullabies for the Film Curious George", "trackCensoredName":"Upside Down",  "artistViewUrl":"https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4",  "collectionViewUrl":"https://itunes.apple.com/us/album/upside-down/id120954021? i=120954025&uo=4", "trackViewUrl":"https://itunes.apple.com/us/album/upside-down/id120954021? i=120954025&uo=4", .... 

但我有一個js錯誤:「未捕獲的SyntaxError:意外的令牌:「在json結果的第一行(」resultCount「:50)。

我嘗試使用transformResponse,但之前發生錯誤。

感謝

回答

2

你忘了添加回調函數(JSON_CALLBACK):

app.controller('Ctrl', function($scope, $http) { 
    $http.jsonp("http://itunes.apple.com/search", { 
     params: { 
      "callback": "JSON_CALLBACK", 
      "term": "jack+johnson" 
     } 
    }).success(function(data, status, headers, config) { 
     console.log("is scope : " + data); 
    }).error(function(data, status, headers, config) { 
     console.log("error" + data); 
    }); 
});​ 

從angularjs $ http.jsonp documentation

url – {string} – Relative or absolute URL specifying the destination of the request. Should contain JSON_CALLBACK string.