gpt4 book ai didi

javascript - 如何在AngularJS中处理http 302响应

转载 作者:行者123 更新时间:2023-12-03 06:53:38 24 4
gpt4 key购买 nike

我有一个java过滤器,它检查 session 属性用户名。当用户名为空时,重定向到路径/login。当用户名为空时,我访问路径/index.html,我得到了HTTP代码302,所以我在angularjs中添加拦截器。但是我访问/index.html时,用户名为null时出现错误。

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 中的错误如下: enter image description here

最佳答案

您无法处理来自服务器的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com