2016-07-06 102 views

回答

0

在一個項目中,我們使用這種截取http錯誤的方式。在那裏你可以重定向到登錄頁面(或任何你喜歡的)。

function httpInterceptor($provide, $httpProvider) { 
    function interceptorFunction($q) { 
     return { 
      responseError: function (rejection) { 
       if (typeof rejection.status !== 'undefined') { 
        if (rejection.status === 401) { 
         //Your app will handle 401 errors here... 
        } 
       } 
       return $q.reject(rejection); 
      } 
     }; 
    } 

    $provide.factory('httpInterceptor', interceptorFunction); 
    interceptorFunction.$injector = ['$q']; 
    $httpProvider.interceptors.push('httpInterceptor'); 

angular.module('app', dependencies) 
    .config(httpInterceptor); 

'app'模塊是我們的'main'模塊。應該發生401錯誤,儘管服務器應至少一次以該狀態碼作爲響應。

+0

好的,謝謝。我其實只是實現了這一點,還沒有測試過它。但是,這是否也會攔截laravel請求?即攔截所有http請求而不是通過$ http服務創建的請求? – Dmac

+0

這肯定會攔截每個$ http請求。我不知道你可以使角度應用程序不會使用$ http的Ajax調用。 $資源是依賴於$ HTTP也......如果你做一個香草的JavaScript調用它可能無法捕捉它(但爲什麼) – Rouz

+0

問題是與laravel @include不與角模板,雖然... – Dmac

相關問題