2017-09-18 3 views
1

내 업로드 - 업데이트 페이지이며 응답이 401 일 때 자동으로 app.inithttp interceptor 코드를 호출하지 않습니다. -------------

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(); 
           }); 

         }); 
      } 
     } 
    } 
]) 

대신 $scope.close() 난 내가 인터셉터를 호출 할 $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

인터셉터가 잘못되었습니다.

$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를 참조하십시오 생성자 함수가 아닌 함수 객체를 반환해야합니다.

관련 문제