我有一个用Angular写的应用。它本质上是一个SPA,除了我的登录是通过传统的非ajax请求完成。我想要的只是在服务器返回401时将页面重定向(完全绕过路由)到/logout。我可以设置一个基于ajax的登录,但坦率地说,我没有看到好处。考虑到这一点,如果它更容易做到这一点-我会做任何事情,只要能让我越过这个疯狂烦人的障碍,这是非常简单的jQuery。下面是我在删除授权cookie时将我放入无限循环的内容:
var manageModule = angular.module('manageModule', ['ngResource']);
manageModule.config(function($httpProvider, $routeProvider, $locationProvider) {
$httpProvider.defaults.headers.put['Content-Type'] =
'application/x-www-form-urlencoded';
$httpProvider.defaults.headers.post['Content-Type'] =
'application/x-www-form-urlencoded';
$locationProvider.html5Mode(false);
$httpProvider.interceptors.push(function($q, $window) {
return {
'responseError': function(rejection) {
var status = rejection.status;
if (status == 401) {
console.log('401 inside if');
$window.location.href = contextPath + '/logout';
}
return $q.reject(rejection);
}
}
});
$routeProvider
.when('/majors', {
templateUrl: contextPath+'/manage/majors',
controller: ManageMajorsController
})
.when('/users', {
templateUrl: contextPath+'/manage/users',
controller: ManageUsersController
})
.when('/courses', {
templateUrl: contextPath+'/manage/courses',
controller: ManageCoursesController
})
.when('/notes', {
templateUrl: contextPath+'/manage/notes',
controller: ManageNotesController
})
.when('/manage', {
templateUrl: contextPath+'/manage/majors',
controller: ManageMajorsController
});
});
谢谢你提供的任何帮助!
这似乎是一些新的行为,我记得它是在一个重定向循环。无论哪种方式,以下是无限输出到控制台的内容。
Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,nc.$evalAsync(function(){c.$broadcast("$locationChangeStart",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 15; oldVal: 14"],["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,nc.$evalAsync(function(){c.$broadcast("$locationChangeStart",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 16; oldVal: 15"],["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,nc.$evalAsync(function(){c.$broadcast("$locationChangeStart",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 17; oldVal: 16"],["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,nc.$evalAsync(function(){c.$broadcast("$locationChangeStart",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 18; oldVal: 17"],["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,nc.$evalAsync(function(){c.$broadcast("$locationChangeStart",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 19; oldVal: 18"]]
at Error (<anonymous>)
at Object.e.$digest (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:91:135)
at Object.e.$apply (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:92:431)
at j (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:101:80)
at r (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:104:449)
at XMLHttpRequest.v.onreadystatechange (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:106:90) bundle-main_defer.js:63
Uncaught Error: 10 $digest() iterations reached.
可能与这个问题有关。
我以前在我的控制器中使用过这个,当时我根本不想使用Angular的路由:
// Abort Angular JS default location change handling.
$scope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
if (newUrl == $location.absUrl()) {
event.preventDefault();
}
});
对于您的情况,看起来您只想保释某些url,因此您可能希望/需要做一些模式匹配来代替newUrl == $location.absUrl()
。
这可能不太适合你,因为我想避开Angular的所有路由机制,而且我不确定它与新版本的兼容性如何。我已经在Angular 1.0中使用了它。x版本。