我使用一个有角度的$ http拦截器来检查ajax请求是否返回401(未经过身份验证).如果响应为401,则原始请求排队,显示登录表单,登录成功后,将重试排队的请求.这已经与$ http一起使用了,角度拦截器的来源是:
define('common.service.security.interceptor', ['angular'], function() {
'use strict';
angular.module('common.service.security.interceptor', ['common.service.security.retryQueue'])
.factory('securityInterceptor', [
'$injector',
'$location',
'securityRetryQueue',
function($injector, $location, securityRetryQueue) {
return function(promise) {
var $http = $injector.get('$http');
// catch the erroneous requests
return promise.then(null, function(originalResponse){
if(originalResponse.status === 401){
promise = securityRetryQueue.pushRetryFn('Unauthorized', function retryRequest(){
return $injector.get('$http')(originalResponse.config);
});
}
return promise;
});
};
}
])
// register the interceptor to the angular http service. method)
.config(['$httpProvider', function($httpProvider) {
$httpProvider.responseInterceptors.push('securityInterceptor');
}]);});
Run Code Online (Sandbox Code Playgroud)
如何使用这个有角度的$ http拦截器创建breeze请求?
Breeze为文件"Breeze/Adapters/breeze.ajax.angular.js"中的angular $ http服务提供了一个包装器.所以第一个想法是告诉微风使用它:
breeze.config.initializeAdapterInstance("ajax", "angular", true);
Run Code Online (Sandbox Code Playgroud)
调试angular.js,它显示breeze现在实际上使用$ http,但它不执行上面注册的拦截器.在$ http里面,有一个数组"reversedInterceptors",它包含已注册的拦截器.我将此数组记录到控制台.如果我使用$ http,则此数组的长度为1(如预期的那样),但在使用breeze发出请求时,此数组为空. …