使用$ http服务,找不到数据

时间:2016-03-26 09:57:01

标签: javascript angularjs json

这是我的代码 -

var app = angular.module('chartApp', []);

app.controller('artistCtrl', ['$scope', '$http', function($scope, $http) {
    $scope.artists = 
    $http({
        method: 'GET',
        url: 'http://ws.audioscrobbler.com/2.0/?method=chart.gettopartists',
        params: {api_key: 'e8452c5962aafbb3e87c66e4aaaf5cbf', format: 'json'}
    }).success(function(result) {
        return result.data;
    });
    console.log($scope.artists);
}]);

如何从json文件中查看原始数据?

2 个答案:

答案 0 :(得分:2)

将赋值语句移动到成功块中:

.success(function(result) {
    $scope.artists = result.data;
})

此外,需要注意的另一件事是console.log($scope.artists)块后面的$http语句仍然会打印undefined,因为它是在解析承诺之前执行的。

答案 1 :(得分:1)

试试这个会起作用:

var app = angular.module('chartApp', []);

app.controller('artistCtrl', ['$scope', '$http', function($scope, $http) {
    $scope.artists = 
    $http({
        method: 'GET',
        url: 'http://ws.audioscrobbler.com/2.0/?method=chart.gettopartists',
        params: {api_key: 'e8452c5962aafbb3e87c66e4aaaf5cbf', format: 'json'}
    }).success(function(result) {
        $scope.artists = result.data;
    }).error(function(msg) {
        $scope.err = msg;
    });
}]);