0
我添加了一個HttpRequestInterceptor
insie我的AngularJS應用程序顯示一個toastr每次請求失敗。HttpRequestInterceptor顯示的倍數乘以相同的錯誤
angular.module('spwApp.factories')
.factory('httpRequestInterceptor', ['$q', '$injector', '$cookies', '$rootScope', function($q, $injector, $cookies, $rootScope) {
return {
'request': function($config) {
var token = $cookies.get('token');
$config.headers['Authorization'] = 'Bearer ' + token;
return $config;
},
'responseError': function(rejection) {
var toastr = $injector.get('toastr');
var $state = $injector.get('$state');
toastr.options = {
"closeButton" : true,
"preventDuplicates" : true,
"timeOut": "50000",
"extendedTimeOut" : "50000"
};
toastr.remove();
switch (rejection.status) {
case 401:
if ($state.current.name != 'login') {
$state.go('login');
toastr.info('Re-enter username/password', 'Invalid sessions', toastr.options);
}
break;
case 403:
toastr.error('You do not have the rights', 'Forbidden', toastr.options)
$state.go('home');
break;
case 404:
toastr.error('Cannot found', '??', toastr.options);
$state.go('home');
break;
case 500:
toastr.error('Unexpected error', 'Hum...', toastr.options);
$state.go('home');
break;
case -1:
toastr.error('Connection to server not possible', 'Ouch...', toastr.options);
$state.go('home');
break;
default:
toastr.error('That is not supposed to land here', 'Oops...', toastr.options);
$state.go('home');
break;
}
return $q.reject(rejection);
}
};
}]);
在特定網頁,我必須從我的服務器解決多個數據
.state('stateA', {
url: '/urlToStateA',
views: {
'[email protected]': {
templateUrl: 'app/stateA.html',
controller: 'controllerA',
controllerAs: 'vm',
resolve: {
dataA: ['myService', function(myService) {
return myService.getDataA();
}],
dataB: ['myService', function(myService) {
return myService.getDataB();
}],
dataC: ['myService', function(myService) {
return myService.getDataC();
}]
}
}
}
})
因此,當我的服務器被關閉,每個請求將獲得rejection.status == -1
,然後顯示toastr Connection to server not possible
問題是行不起作用。它應該刪除所有的命令,但什麼都不做。
如何在添加新托盤之前刪除托盤?我可以使用一些純JavaScript替換非工作的toastr.remove()
手動選擇並刪除toastr?
完美謝謝! – Weedoze
很高興爲您效勞。祝你有美好的一天 :) – Sravan