angularJS 中的 $http.get 错误——成功不是函数 [英] Error with $http.get in angularJS -- Success not a Function

查看:27
本文介绍了angularJS 中的 $http.get 错误——成功不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现此错误:

<块引用>

angular.min.js:122 TypeError: $http.get(...).success 不是函数在 getUserInfo (app.js:7)在新的 (app.js:12)在 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 = '';函数 getUserInfo($scope, $http){$http.get('https://api.github.com/users').成功(功能(结果){$scope.user = 结果;控制台日志(结果);});};getUserInfo($scope, $http);}]);

这里是 html

<html ng-app="gitHub"><头><title>Github 用户目录</title><script src="angular.min.js"></script><script src="app.js"></script><身体><div ng-controller="mainController"><div><h1>GitHub 用户</h1>你想搜索谁?<button ng-click="getUserInfo()">搜索</button>

<div>{{ 用户 }}

</html>

解决方案

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

$http.get('https://api.github.com/users').then(功能(响应){var data = response.data;var status = response.status;var statusText = response.statusText;var headers = response.headers;var config = response.config;$scope.user = 数据;控制台日志(数据);});

<块引用>

弃用通知

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

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

另请参阅SO:为什么不推荐使用 angular $http 成功/错误方法?.

Getting this error:

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

Here is my code:

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);
}]);

and here is the 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>

解决方案

The .success and .error methods are deprecated and have been removed from AngularJS 1.6. Use the standard .then method instead.

$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.

Also see SO: Why are angular $http success/error methods deprecated?.

这篇关于angularJS 中的 $http.get 错误——成功不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆