2014-10-29 70 views
4

我已經創建了一個簡單的攔截器來將用戶重定向到登錄頁面,服務器返回401錯誤。它不工作,因爲狀態被設置爲404,所以$ location永遠不會被調用。AngularJs httpprovider.interceptors responseError的狀態不正確

.config(function myAppConfig($stateProvider, $urlRouterProvider, $httpProvider) { 
    $urlRouterProvider.otherwise('/login/authorize'); 

    $httpProvider.interceptors.push(['$location', function($location) { 
     return { 
      responseError: function(rejection) { 
       console.log(rejection); 
       switch (rejection.status) { 
        case 401: 
         $location.path('/login/refresh'); 
       } 
      } 
     }; 
    }]); 
}) 

enter image description here

+0

你解決了這個問題嗎?在我的情況下,拒絕變量根本沒有被填充。我的拒絕對象看起來像這樣:{data:「」,status:0,headers:function,config:Object,statusText:「」} – zszep 2014-12-07 17:46:24

+0

不,我不得不拋開這個問題,因爲我沒有得到它的任何地方,需要轉移到其他問題。當我回到它併發布我在這裏找到的任何解決方案時。 – mike 2014-12-09 18:39:49

+0

:-)所以做一每當我從我的nancyfx模塊發送一個401錯誤對象未填充,所以我在角代碼,狀態代碼0是401 – zszep 2014-12-09 18:43:29

回答

2

我不知道這是否是您的情況@mike的情況下,你實際上得到一個錯誤代碼 - 只是沒有合適的人......但或許下面是相關...

關於@zszep及其狀態爲0的問題,我也遇到了這個問題,我相信這可能是由於您的服務器發出的錯誤響應沒有存在CORS頭文件 - https://github.com/angular/angular.js/issues/3336

這就是我的情況 - 只是想在這裏發佈它,以免它幫助任何人。