gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:18:40 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why are AngularJS $http success/error methods deprecated? Removed from v1.6?

(2 个回答)


2年前关闭。




收到此错误:

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/41183845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com