angularjs - angularJS 中的 $http.get 错误 - 成功不是函数

标签 angularjs angularjs-http angularjs-1.6

出现此错误:

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) at Q.instance (angular.min.js:93) at p (angular.min.js:68) at g (angular.min.js:60) at g (angular.min.js:61) at g (angular.min.js:61) at angular.min.js:60 at 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>

最佳答案

.success.error 方法已弃用,并已 removed from 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);
});

Deprecation Notice

The $http legacy promise methods .success and .error have been deprecated and will be removed in v1.6.0. Use the standard .then method instead.

— AngularJS (v1.5) $http Service API Reference -- Deprecation Notice.

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

关于angularjs - angularJS 中的 $http.get 错误 - 成功不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56900212/

相关文章:

javascript - $scope.apply 正在触发 "catch"以及 "then"

angularjs - 为什么 AngularJS $http success/error 方法被弃用?从 v1.6 中删除?

javascript - 替换 javascript 或 angularjs 中字符串中某些字符的存在

javascript - 尝试获取 DropDown 值

javascript - AngularJS 在 $http 调用后添加到 JSON 对象

angularjs - 在 Angularjs 应用程序中使用 $rootscope 的最佳实践?

angularjs - AngularJS 中的 401 未经授权的错误处理

javascript - 从 addEventListener 函数向 ng-repeat 添加数据不会更新 DOM

angularjs - $http 问题 - 在 md-autocomplete Angular Material 中解决 promise 之前无法返回值

javascript - 每个循环内链接的 angularjs $http 请求