我有一个Java过滤器,用于检查会话属性用户名。当用户名为null时,重定向到路径/ login。当用户名为null时,我访问路径/index.html,我得到了HTTP代码302,因此我在angularjs中添加了拦截器。但是当用户名为null时,我访问/index.html时出错。



var testApp = angular.module('testApp', [ 'ngRoute', 'myApp' ]);

testApp.config([ '$routeProvider', function($routeProvider) {
	$routeProvider.when('/anchor/historyAttendance/:uid',{
        templateUrl : 'anchor/historyAttendance.html',
        controller : 'AnchorHistoryAttendanceCtrl'
    }).when('/anchor/list', {
        templateUrl : 'anchor/list.html',
        controller : 'AnchorListCtrl'
    }).otherwise({
		redirectTo : '/'
	});
} ]);

var app = angular.module('myApp', [ 'ngTable', 'ngFileUpload', 'ngDialog' ,'ui.colorpicker', 'ngCsv', 'ngSanitize'],function ($provide,$httpProvider) {
    // register the interceptor as a service
    $provide.factory('myHttpInterceptor', function($q) {
        return {
            // optional method
            'request': function(config) {
                // do something on success
                console.log(config);
                return config;
            },
            // optional method
            'requestError': function(rejection) {
                // do something on error
                console.log(rejection);
                return $q.reject(rejection);
            },
            // optional method
            'response': function(response) {
                // do something on success
                console.log(response);
                return response;
            },
            // optional method
            'responseError': function(rejection) {
                // do something on error
                console.log(rejection);
                return $q.reject(rejection);
            }
        };
    });

    $httpProvider.interceptors.push('myHttpInterceptor');
});

app.directive('fontColor', function () {
    return {
        restrict: 'E',
        scope: {},
        replace: false,
        template: '<div color-picker default-color="#ff0000" class="font-color" ng-style="{\'background-color\': selectedFontColor}"></div>',
        link: function (scope) {
            scope.selectedFontColor = '#f00';
            scope.$on('colorPicked', function (event, color) {
                scope.selectedFontColor = color;
            });
        }
    }
});




chrome中的错误是这样的:javascript - 如何在angularjs中处理HTTP 302响应-LMLPHP

最佳答案

您无法处理来自服务器的302响应,因为浏览器会在通知Angular之前执行此操作。从某种意义上说,Angular响应拦截器永远不会帮助您。

在这里正确解释:Handle HTTP 302 response from proxy in angularjshttps://stackoverflow.com/a/29620184/2405040

关于javascript - 如何在angularjs中处理HTTP 302响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37387250/

10-12 15:19