即使状态为Unauthorized 403,$ http.post也会输入成功功能

时间:2015-07-13 06:44:59

标签: angularjs request

我有以下代码用于登录表单:

function(loginData) {
    var deferred = $q.defer();
    var data = {
        "email": loginData.email,
        "password": loginData.password,
        "remember": loginData.remember
    };
    $http.post(CONSTANTS.baseURL + '/login', data)
        .success(function(data, status, headers, config) {
            _authenticate(data);
            //redirects to home
            $state.go('home');
            // location.hash = "#/home";
            deferred.resolve(true);
        })
        .error(function(data, status, headers, config) {
            //show error message
            _logout();
            deferred.resolve(false);
            return false;
        });
    return deferred.promise;
};

调试代码始终会触发.success函数。 请求的状态是403. .success函数的状态参数也是403

Request Method:POST
Status Code:403 Forbidden

2 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,发现原因是因为我之前配置了$ httpProvider拦截::

app.config(function ($httpProvider) {
    $httpProvider.interceptors.push('myHttpInterceptor');
});

删除它后,我的错误回调被正确调用。

另一个参考:https://github.com/angular/angular.js/issues/2609

答案 1 :(得分:0)

您可以尝试以下操作:

$httpProvider.interceptors.push(function ($q, $rootScope) {
    return {
        'responseError': function(rejection) {
            switch (rejection.status) {
                case 500:
                    $rootScope.$broadcast('server:error');
                    break;
            }
            return $q.reject(rejection); //<-important
        }
    }
});