gpt4 book ai didi

javascript - AngularJS - 引用错误 : success is not defined

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:11 25 4
gpt4 key购买 nike

当从 Controller 向我的 node.js 后端执行 Restful 调用时,我收到“ReferenceError: success is not defined”,如下所示:

authControllers.js:

authControllers.controller('authCtrl', ['$scope', '$location', '$window', 'UserService', 'AuthenticationService',
function authCtrl($scope, $location, $window, UserService, AuthenticationService) {
$scope.me = function() {
UserService.me(function(res) {
$scope.myDetails = res;
}, function() {
console.log('Failed to fetch details');
$rootScope.error = 'Failed to fetch details';
})
};
}]);

authServices.js:

authServices.factory('UserService',['$http', function($http) {
return {
me:function() {
return $http.get(options.api.base_url + '/me').success(success).error(error)
}
}
}]);

html:

<div class="row" data-ng-controller="authCtrl" data-ng-init="me()">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading">
<strong>Your Details</strong>
</div>
<div class="panel-body">
<p>{{myDetails.data.username}}</p>
<p>{{myDetails.data.email}}</p>
</div>
</div>
</div>
</div>

正在接收一个成功的 nodeJs 调用并且它也在返回数据,但是无法在前端获取它。请帮忙!

最佳答案

错误是:

return $http.get(options.api.base_url + '/me').success(success).error(error)

要修复它,请不要尝试引用不存在的 successerror 函数。

return $http.get(options.api.base_url + '/me')

当您正确使用 $http 时,Angular 会返回一个 $q promise。成功和错误解决方法已弃用。

The $http legacy promise methods success and error have been deprecated.

关于javascript - AngularJS - 引用错误 : success is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33322834/

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