当Angular非200时,Angular没有得到回应

时间:2014-07-29 13:49:46

标签: javascript angularjs authentication cordova

我发现httpInterceptor找到了here

如果我的Basic Auth标头有有效的凭据,一切正常,但是如果我找回401,那么应用程序就会挂起而且我从未收到回复。

这是我的拦截器:

angular.module('TDE').factory('httpInterceptor', function httpInterceptor ($q, $window, $location) {
    return function (promise) {
        var success = function (response) {
            //window.logger.logIt("httpInterceptor received a good response: " + response);
            return response;
        };

        var error = function (response) {
            //window.logger.logIt("httpInterceptor received an error: " + response);

            if (response.status === 401) {
                $location.url('/login');
            }

            return $q.reject(response);
        };

        return promise.then(success, error);
    };
});

在app.js中声明httpInterceptor

angular.module('TDE', []);

var TDE = angular.module('TDE', ['ux', 'ngRoute', 'ngResource', 'TDE', 'hmTouchEvents', 'infinite-scroll', 'ui.bootstrap', 'ui.sortable']);

TDE.config(['$routeProvider', '$locationProvider', '$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) {
    $httpProvider.responseInterceptors.push('httpInterceptor');

    $routeProvider
        .when('/', {})
        .when('/login', { templateUrl: "Views/Login/login.html", controller: "LoginController" })

我的身份验证方法

authenticate: function (user, password) {
//            window.logger.logIt("serviceAccount: " + $rootScope.serviceAccount);
            window.logger.logIt("In authenticate...");

            var deferred = $q.defer();
            var encoded = encoder.encode($rootScope.serviceAccount);
            //var encoded = encoder.encode(user + ":" + password);

            if (user && password) {
                window.logger.logIt("We've got a username and password...");

                $http.defaults.headers.common.Authorization = 'Basic ' + encoded;

                sessionStorage.setItem('Authorization', $http.defaults.headers.common.Authorization);

                var url = $rootScope.serviceBaseUrl + "login/authenticate";

                window.logger.logIt(url);

                $http({
                    method: "POST",
                    url: url,
                    data: {
                        "Username": user,
                        "Password": password,
                        "AccessToken": ""
                    },
                    headers: {
                        "Content-Type": "application/json"
                    }
                })
                .success(function (data, status, headers, config) {
                    window.logger.logIt("We've got a response (success)...");

                    if (data.IsAuthenticated) {
                        deferred.resolve(data);
                        session.setSession();
                    } else {
                        deferred.reject(status);
                    }
                })
                .error(function (data, status, headers, config) {
                    window.logger.logIt("We've got a response (error)...");

                    $dialogservice.showMessage("Authentication Error", "Return Code: " + status);
                    deferred.reject(status);
                });
            } else {
                window.logger.logIt("We've got a response...");

                deferred.reject(401);
            }

            return deferred.promise;
        },

您会在我的Authenticate方法中看到,我测试了两行:

var encoded = encoder.encode($rootScope.serviceAccount);

var encoded = encoder.encode(user + ":" + password);

我们需要使用基本身份验证(通过SSL)。现在,我所有的测试都是我可以收回401。如果我使用$ rootScope.serviceAccount(工作正常),我会立即收到200响应。但是,如果我故意发送一个错误的用户名/密码,我永远不会得到回应,应用程序就在那里。

编辑:好的,我已将我的代码更新为以下内容,但仍然采取相同的行为:

angular
    .module('TDE')
    .config(['$httpProvider', function ($httpProvider) {

    $httpProvider.interceptors.push(function ($q) {
        return {
            'request': function (config) {
                window.logger.logIt("Request is being sent...");

                var headers = config.headers;

                if (!headers.Authorization) {
                    headers.Authorization = sessionStorage.getItem('Authorization');
                }

                return config || $q.when(config);
            },

            'response': function (response) {
                window.logger.logIt("got a good response...");

                return response;
            },

            'responseError': function (rejection) {
                window.logger.logIt("responseError error...");

                return $q.reject(rejection);
            },
        };
    });
}]);

2 个答案:

答案 0 :(得分:2)

嗯,再次,PhoneGap就是问题!!!!!!!!!!!!!!!!!

Example 1

Example 2

Example 3

Example 4

答案 1 :(得分:0)

试试这个拦截器:

.factory('httpInterceptor', function(){         
     return {
         request : function(yourRequestConfig){

            var returnPromise = $q.defer();

            $http(yourRequestConfig).then(function(response) {
                console.log("successful response from server ", response);
                returnPromise.resolve(response);

            }, function(someReason) {
                console.log("failure response from server", reason);
                returnPromise.reject(reason);
            });
            return returnPromise.promise;
         }
     }
});

用作

httpInterceptor.request(request config).then(returnValue){
    console.log('inside controller', returnValue);
});

其中请求配置类似于:

var requestConfig = {
    method: "GET",
    url: "localhost:8080/getStuff"
};