angularJS中的$ http.get错误-成功不是函数 [英] Error with $http.get in angularJS -- Success not a Function
问题描述
得到此错误:
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)在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
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>
推荐答案
。成功
和 .error
方法已被弃用,并已已从AngularJS 1.6中删除。改用标准的 .then
方法。
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);
});
弃用通知
$ http
传统承诺方法.success
和.error
已被弃用,并将在v1.6.0中删除。改为使用标准的.then
方法。
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服务API参考-弃用声明。
另请参见 SO:为什么$ http成功/ error方法已弃用?。
这篇关于angularJS中的$ http.get错误-成功不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!