$ http.post的angularjs服务没有输入成功或错误回调

时间:2015-11-01 16:09:27

标签: javascript angularjs angular-fullstack

我有一个由angular-fullstack yeoman generator

生成的meanjs应用程序

并在其中使用像这样的身份验证角色服务 -

'use strict';

angular.module('app')
    .factory('Auth', function Auth($location, $rootScope, $http, User, $cookieStore, $q) {
        var currentUser = {};
        if ($cookieStore.get('token')) {
            currentUser = User.get();
        }

        return {

            login: function (user, callback) {
                var cb = callback || angular.noop;
                var deferred = $q.defer();
                $http.post('/auth/local', {
                    email: user.email,
                    password: user.password
                }).
                    success(function (data, status, headers, config) {
                        $cookieStore.put('token', data.token);
                        currentUser = User.get();
                        deferred.resolve(data);
                        return cb();
                    }).
                    error(function (data, status, headers, config) {
                        this.logout();
                        deferred.reject(data);
                        return cb(data);
                    }.bind(this));

                return deferred.promise;
            },

            /**
             * Delete access token and user info
             *
             * @param  {Function}
             */
            logout: function () {
                $cookieStore.remove('token');
                currentUser = {};
            },

            getToken: function () {
                return $cookieStore.get('token');
            }
        };
    });

所以,基本上在我的登录页面控制器上我正在做这样的事情 -

$scope.login = function () {
        $scope.authError = null;
        // Try to login
        Auth.login({
            email: $scope.user.email,
            password: $scope.user.password
        })
            .then(function (data) {
                // Logged in, redirect to home
                console.log(data, 'gee');
                $state.go('app.dashboard-v1');
            })
            .catch(function (err) {
                $scope.authError = 'Email or Password not right';
            });
    };

所以,我试图将console.log('something');语句放在我的服务中的成功和错误回调中,但在任何情况下都没有打印。我发送的POST http://localhost:9000/auth/local 405 (Method Not Allowed)表示我的错误凭据是405状态代码,200正是凭正确的凭据发送。那么,我应该更改我的客户端服务代码或控制器代码或服务器端状态代码来解决这个问题吗?

[UPDATE] 我试过@Bastien建议的答案 并将我的控制器代码更改为 -

Auth.login({
            email: $scope.user.email,
            password: $scope.user.password
        })
            .then(function (data) {
                // Logged in, redirect to home
                $state.go('app.dashboard-v1');
                toaster.pop('success','message','message');
                console.log(data, 'gee');
            },function(err){
                $scope.authError = 'Email or Password not right';
                console.log('err');
            })

但是我没有在浏览器控制台上收到任何消息,我也注意到只有在输入错误的凭据时我才会在浏览器控制台中再出现一个错误,如下所示 -

TypeError: Cannot read property 'data' of undefined
    at angular.js:9408
    at processQueue (angular.js:13248)
    at angular.js:13264
    at Scope.parent.$get.Scope.$eval (angular.js:14466)
    at Scope.parent.$get.Scope.$digest (angular.js:14282)
    at Scope.parent.$get.Scope.$apply (angular.js:14571)
    at done (angular.js:9698)
    at completeRequest (angular.js:9888)
    at XMLHttpRequest.requestLoaded (angular.js:9829)

是否与此有关?

1 个答案:

答案 0 :(得分:0)

你有没有尝试过:

Auth.login({
    email: $scope.user.email,
    password: $scope.user.password
})
.then(function (data) {
    // Logged in, redirect to home
    console.log(data, 'gee');
    $state.go('app.dashboard-v1');
},
function(error){ //2nd function to handle the error (!= 200)
    $scope.authError = 'Email or Password not right';
});

方法“then”有多个参数:第一个用于成功处理程序,第二个用于错误处理程序