Sri*_*ri7 3 javascript angularjs
当从控制器到我的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';
})
};
}]);
Run Code Online (Sandbox Code Playgroud)
authServices.js:
authServices.factory('UserService',['$http', function($http) {
return {
me:function() {
return $http.get(options.api.base_url + '/me').success(success).error(error)
}
}
}]);
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
正在接收一个成功的nodeJs调用,它也返回数据,但无法在前端获取它.请帮忙 !
错误是:
return $http.get(options.api.base_url + '/me').success(success).error(error)
Run Code Online (Sandbox Code Playgroud)
要修复它,请不要尝试引用success和error不存在的函数.
return $http.get(options.api.base_url + '/me')
Run Code Online (Sandbox Code Playgroud)
$q当你调用时$http,Angular会返回一个你正确使用的承诺.不推荐使用成功和错误解决方法.
$ http遗留承诺方法成功和错误已被弃用.