angularJS中$ http.get出错 - 成功不是函数

Man*_*han 26 angularjs angularjs-http angularjs-1.6

得到此错误:

angular.min.js:122 TypeError:$ http.get(...).成功不是getUserInfo(app.js:7)中的函数,在new(app.js:12)处于Object.invoke(angular.min) .js:43)在Q.instance(angular.min.js:93)at p(angular.min.js:68)at g(angular.min.js:60)at g(angular.min.js:61) )在angular(min.min.js:61)处于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);
}]);
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

geo*_*awg 59

.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);
});
Run Code Online (Sandbox Code Playgroud)

弃用通知

$http传统方法的承诺.success,并.error已被弃用,并将在V1.6.0被删除.请改用标准.then方法.

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

另请参阅SO:为什么angular $ http成功/错误方法已弃用?.


dev*_*ero 15

我认为你在使用角度时需要使用.then而不是.success .

来自doc的示例

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

以下是$ 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.
  });
Run Code Online (Sandbox Code Playgroud)

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

$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);
        });
};
Run Code Online (Sandbox Code Playgroud)

  • 他们不需要**来使用`.then()`尽管他们应该弃用`.success()`.你应该给出一个例子,虽然它使用`$ http`和`.then()`而不是通用的promise例子. (2认同)