【发布时间】:2014-11-25 07:52:22
【问题描述】:
有如下代码:
angular.module('app.services', []).factory('authService', [
'SIGN_IN_ENDPOINT', 'SIGN_OUT_ENDPOINT', '$http', '$cookieStore', function(SIGN_IN_ENDPOINT, SIGN_OUT_ENDPOINT, $http, $cookieStore) {
var auth;
auth = {};
auth.signIn = function(credentials) {
return $http.post(SIGN_IN_ENDPOINT, {
user: credentials
}).then(function(response, status) {
return $cookieStore.put('user', response.data);
}, function(error) {
return console.log("Incorrect email/password");
});
};
return auth;
}
这是我的身份验证模块。现在我在控制器中有以下功能:
angular.module('app.admin.controllers', []).controller('SignInController', [
'$scope', 'authService', '$state', function($scope, authService, $state) {
$scope.buttonText = "Login";
return $scope.login = function() {
$scope.buttonText = "Logging in. . .";
return authService.signIn($scope.credentials).then(function(response, status) {
return $state.go('allPosts');
}, function(err) {
$scope.invalidLogin = true;
return $scope.buttonText = "Login";
});
};
}
问题:如果我输入了错误的电子邮件/密码,我正在等待 2 个错误回调 - 从第一个“then”到第二个,但我捕获了第一个错误回调(我可以看到控制台日志)和之后它执行成功回调! (返回 $state.go('allPosts'))。为什么?来自服务器的响应是 401 错误。
【问题讨论】:
标签: javascript angularjs