2015-04-02 77 views

回答

0

你可以檢查您是否在線或者通過閱讀在你的攔截器的響應的狀態,如果其501分之401的/ etc:

var interceptor = ['$rootScope', '$q', function ($rootScope, $q) { 
    function success(response) { 
     return response; 
    } 
    function error(response) { 
     var status = response.status; // error code 
     if ((status >= 400) && (status < 500)) { 
     $rootScope.broadcast("AuthError", status); 
     return; 
     } 
     if ((status >= 500) && (status < 600)) { 
     $rootScope.broadcast("ServerError", status); 
     return; 
     } 
     // otherwise 
     return $q.reject(response); 
    } 
    return function (promise) { 
     return promise.then(success, error); 
    } 
}] 

還有另外一種方式,使用HTML5,但我猜在某些瀏覽器上不起作用。這是使用navigator.onLine屬性來實現,如:

if (navigator.onLine) { 
    //I'm online 
} else { 
    I'm not online 
} 

https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine

0

如果你把你的請求攔截器內的對象,這將觸發對responseError攔截通話,傳遞扔對象作爲其論據。

你必須找到一個方法通知responseError攔截器,這不是一個真正的錯誤,它應該返回一個自定義的響應。

的responseError攔截器可以選擇恢復錯誤返回響應對象o失敗返回拒絕承諾(查看角的$ HTTP攔截文檔)

編輯:如果您的請求Interceptor拋出一個對象,你不能避免控制檯中的錯誤消息。最好是返回一個被拒絕的promise傳遞配置對象作爲值,並在其中放置額外的信息,以便responseError檢測特殊情況。