这是我的上传 - 更新页面,当响应为401时,它不会自动调用app.init
http interceptor代码。相反$scope.close()
我尝试了很多我知道我必须改变$scope.close()
来调用拦截器但需要改变我没有得到其他场景的自动拦截器:
angular.module('Myapp')
.controller('CatalogueUploadDialogCtrl', [ '$scope', '$window', '$location', '$log', 'dialog', 'UiService', 'I18nService', 'ApiErrorHandlerService', 'EntitySaveService',
function($scope, $window, $location, $log, dialog, uiService, i18n, apiErrorHandler, entitySaveService) {
$scope.close = function() {
dialog.close();
}
$scope.isOpenDisabled = function() {
return ($scope.formController && $scope.formController.inProgress())
|| !($scope.model && $scope.model.file && $scope.model.file.name);
}
$scope.open = function($event) {
if(!$scope.isOpenDisabled()) {
entitySaveService.flush()
.then(function() {
$scope.formController.submit()
.then(
function success(classLibraryHandle) {
$scope.classLibraryHandle = classLibraryHandle;
$scope.close();
},
function (response) {
// I've found no good way to reset the formController;
// closing the dialog on error.
$scope.close();
});
});
}
}
}
])
---------------- app.init.js页面中的Http拦截器代码页面-------------
.config([ '$httpProvider', function($httpProvider, i18n) {
var handling401;
// Add header to allow server side to detect XHR request
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$httpProvider.responseInterceptors.push([ '$q', '$window', '$log', '$injector', '$rootScope', '$timeout', 'I18nService', 'ApiErrorHandlerService', 'SessionService',
function ($q, $window, $log, $injector, $rootScope, $timeout, i18n, apiErrorHandler, session) {
return function (promise) {
return promise.then(
function httpSuccess(response) {
if(response && response.data && response.data === 'null') {
response.data = null;
}
return response;
},
function httpError(response) {
function resolveMessage(data) {
var msg;
_.each([ 'h1', 'h2', 'h3', 'h4', 'title'], function(tag) {
var pattern = '<' + tag + '[^>]*>([^<]*)';
var match = new RegExp(pattern).exec(data);
if(match) {
msg = match[1];
}
});
return msg;
}
// Identity Provider Token timeout handling
var innerScope, $dialog;
if(response.status === 401 || response.status === 404 && response.data === '') {
if(!handling401) {
// The token refresh will cause a new 401. Because of this
// we need to prevent 401 handling while a 401 handle is
// in progress.
handling401 = true;
innerScope = $rootScope.$new();
innerScope.error = resolveMessage(response.data);
$dialog = $injector.get('$dialog');
$dialog.dialog({
// We need to keep this HTML inline because if the
// client authentication token have timed out, we will
// be unable to query the view using templateUrl
template: '<div class="modal-header"> \
<h3>{{ \'authentication-failed.dialog.header\' | i18n }}</h3> \
</div> \
<div class="modal-body"> \
<div class="modal-body-inner"> \
<div ng-show="!loaded"> \
<i class="icon-spinner"></i> {{ \'authentication-failed.dialog.refreshing\' | i18n }} \
</div> \
<div ng-show="iframe.status == \'fail\'" class="alert alert-error"> \
<h4>{{ \'authentication-failed.dialog.refresh-failed\' | i18n }}</h4> \
<span ng-if="error"><small>{{ \'reason\' | i18n }}:<br />{{ error }}</small></span> \
</div> \
<div ng-show="iframe.status == \'success\'" class="alert alert-info"> \
<h4>{{ \'authentication-failed.dialog.refresh-successful\' | i18n }}</h4> \
</div> \
<iframe av-authentication-iframe="iframe.status" style="width:500px;height:100px;display:none"></iframe> \
</div> \
</div> \
<div class="modal-footer"> \
<button type="button" ng-if="iframe.status == \'fail\'" onclick="location.reload(false)" class="btn">{{ \'reload\' | i18n }}</button> \
<button type="button" ng-if="iframe.status == \'success\'" ng-click="close()" class="btn">{{ \'close\' | i18n }}</button> \
</div>',
controller: 'AuthenticationFailedDialogCtrl',
resolve: {
$scope: function() {
return innerScope;
}
}
}).open()
.finally(function() {
innerScope.$destroy();
innerScope = null;
handling401 = false;
});
}
return $q.reject(response);
}
// License handling
if(response.status === 402) {
if(!$window.licenseDialogOpen) {
$window.licenseDialogOpen = true;
innerScope = $rootScope.$new();
innerScope.error = response.data;
$dialog = $injector.get('$dialog');
$dialog.dialog({
templateUrl: 'views/license-invalid-dialog.html',
controller: 'DialogCtrl',
resolve: {
$scope: function() {
return innerScope;
}
}
}).open()
.finally(function() {
innerScope.$destroy();
innerScope = null;
$window.licenseDialogOpen = false;
});
}
return $q.reject(response);
}
if (response.status === 409 && apiErrorHandler.isExtensionNamespaceError(response.data)) {
if(apiErrorHandler.isError(response.data)) {
apiErrorHandler.handle(response.data);
}
return $q.reject(response.status);
}
$log.error('--> Intercepted failed HTTP response', response);
if(response) {
if(apiErrorHandler.isError(response.data)) {
apiErrorHandler.handle(response.data);
}
// Ignore the error if it's an IgnoreError instance
else if(response.data != apiErrorHandler.IgnoreError()) {
if(response.data && response.data.ExceptionMessage) {
$window.alert('ExceptionType: ' + response.data.ExceptionType + '\n\n' +
'ExceptionMessage: ' + response.data.ExceptionMessage + '\n\n');
} else {
if(response.status === 0) {
$log.info('Ignoring aborted XHR request.');
} else {
var msg = 'An unknown error with status "' + (response.status || response) + '" occurred.';
if(response && response.data) {
if(typeof response.data == 'string') {
msg = resolveMessage(response.data);
} else if(response.data.Message) {
msg = response.data.Message;
if(response.data.MessageDetail) {
msg += "\n\n" + response.data.MessageDetail;
}
}
}
$log.error(msg);
$window.alert(msg);
}
}
}
}
return $q.reject(response);
});
};
答案 0 :(得分:0)
拦截器格格不入。构造函数应返回一个对象,而不是函数:
$httpProvider.responseInterceptors.push([ '$q', '$window', '$log', '$injector', '$rootScope', '$timeout', 'I18nService', 'ApiErrorHandlerService', 'SessionService',
function ($q, $window, $log, $injector, $rootScope, $timeout, i18n, apiErrorHandler, session) {
̶r̶e̶t̶u̶r̶n̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶(̶p̶r̶o̶m̶i̶s̶e̶)̶ ̶{̶
return {
// optional method
'request': function(config) {
// do something on success
return config;
},
// optional method
'requestError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},
// optional method
'response': function(response) {
// do something on success
return response;
},
// optional method
'responseError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
}]);
有关详细信息,请参阅AngularJS $http Service API Reference - Interceptors。