27

收到此错误:

angular.min.js:122 TypeError: $http.get(...).success is not a function at getUserInfo (app.js:7) at new (app.js:12) at Object.invoke (angular.min .js:43) 在 Q.instance (angular.min.js:93) 在 p (angular.min.js:68) 在 g (angular.min.js:60) 在 g (angular.min.js:61) ) 在 g (angular.min.js:61) 在 angular.min.js:60 在 angular.min.js:21

这是我的代码:

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

gitHub.controller('mainController', ['$scope', '$http', function($scope, $http) {

    var $scope.user = '';
    function getUserInfo($scope, $http){ 
        $http.get('https://api.github.com/users')
            .success(function (result) {
                $scope.user = result;
                console.log(result);
            });
    };
    getUserInfo($scope, $http);
}]);

这是html

<!DOCTYPE html>
<html ng-app="gitHub">
<head>
    <title>Github Users Directory</title>
    <script src="angular.min.js"></script>
    <script src="app.js"></script>
</head>
<body>
    <div ng-controller="mainController">
        <div>
            <h1>GitHub Users</h1>
            Who do you want to search for?<input type="text" name="FindHim" ng-model="queryName" />
            <button ng-click="getUserInfo()">Search</button>
        </div>
        <div>
            {{ user }}
        </div>

    </div>
</body>
</html>
4

9 回答 9

60

.success.error方法已被弃用,并已从AngularJS 1.6中删除。请改用标准.then方法。

$http.get('https://api.github.com/users')
  .then(function (response) {

    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;

    $scope.user = data;
    console.log(data);
});

弃用通知

$http遗留的Promise 方法已被弃用,.success并将.error在 v1.6.0 中删除。请改用标准.then方法。

— AngularJS (v1.5) $http 服务 API 参考——弃用通知

另请参阅SO:Why are angular $http success/error methods deprecated? .

于 2016-12-16T15:18:46.097 回答
15

我认为你需要使用 .then 而不是 .success 使用angular时。

文档中的示例

var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
  alert('Success: ' + greeting);
}, function(reason) {
  alert('Failed: ' + reason);
}, function(update) {
  alert('Got notification: ' + update);
});

这是 $Http 如何使用它的示例:

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

最后你的代码看起来像这样

$scope.getUserInfo = function () {
    $http.get('https://api.github.com/users')
        .then(function (result) {
            $scope.user = result;
            console.log(result);
        }, function(result) {
            //some error
            console.log(result);
        });
};
于 2016-12-16T11:58:42.377 回答
4

这行得通

https://docs.angularjs.org/api/ng/service/$http

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
于 2017-02-14T19:35:24.993 回答
2

根据您当前的实现,您没有将参数(即$scope$http)传递给getUserInfofrom,ng-click="getUserInfo()"因此您会收到错误消息。

您不需要将这些作为参数传递$scope$http因为它已经注入控制器并在$scope.

gitHub.controller('mainController', ['$scope', '$http', function($scope, $http) {

    $scope.user = '';
    //Redefined function, without arguments
    $scope.getUserInfo = function (){ 
        $http.get('https://api.github.com/users')
            .success(function (result) {
                $scope.user = result;
                console.log(result);
            });
    };
    $scope.getUserInfo();
}]);
于 2016-12-16T11:59:40.080 回答
1

你不需要注入 $scope, $http..

app.controller('MainController', function($scope, $http) { 
  $scope.fetchData = function(_city){
    $http.get("../api/AllPlaces?filter[where][placeCity]="+ _city)
    .then(function(response) {
      $scope.Data = response.data;
    });
  }
});
于 2017-07-20T11:24:08.073 回答
0

无需将 $http 作为函数参数传递,因为您已经将 $http 作为依赖项注入到控制器中。我对代码做了一些修改。请检查它是否适合您。

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

gitHub.controller('mainController', ['$scope', '$http', function ($scope, $http) {

    $scope.user = '';

    $scope.getUserInfo = function() {
        $http.get('https://api.github.com/users')
            .success(function (result) {
                $scope.user = result;
                console.log(result);
            });
    };
    $scope.getUserInfo();
}]);
于 2016-12-16T12:11:30.837 回答
0
$http({
    method: 'GET',
    url: '....',
    headers: {
        'Authorization': 'Bearer ' + localStorage["token"]
    }
})
.then(function (data, status, headers, config) {
     alert(JSON.stringify(data) + "Status" + status);
})
.error(function (data, status, headers, config) {
     alert(JSON.stringify(data) + "Status" + status);
});
于 2017-07-07T11:21:36.017 回答
0

根据 Angular JS$http 文档,该系统已被排除在外1.4.3 +所以,我从他的帖子中获得了帮助,您可以尝试这种方式

app.controller('MainCtrl', function ($scope, $http){
   $http({
      method: 'GET',
      url: 'api/url-api'
   }).then(function (success){

   },function (error){

   });
}

或者

$http.get('api/url-api').then(successCallback, errorCallback);

function successCallback(response){
    //success code
}
function errorCallback(error){
    //error code
}

我更喜欢第二个,它对我来说更灵活。

于 2017-11-24T07:04:17.163 回答
-1
function successCallback(response) {
return response
}
$http.get('url')
.then(successCallback)
于 2017-07-31T10:50:43.773 回答