2014-09-22 73 views
-1

我有我使用的URL給出了200 OK狀態和JSON有效負載{error:「Invalid user」}當你沒有提供正確的用戶一個GET請求。在Restangular中使用這個URL時,我寧願這樣做會導致錯誤,所以我可以用承諾以典型的方式處理錯誤,否則我的代碼會非常混亂。我將如何做到這一點?Restangular:當請求失敗時,200 OK響應包含特定的有效負載

+0

哪裏是你的代碼?無論如何,Http結果取決於您的後端代碼 – 2014-09-22 02:04:15

回答

1

您可以指定一個響應攔截這樣的:

app.config(function(RestangularProvider) { 

    // add a response intereceptor 
    RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) { 
    // check the response if it contains error or not. 
     if (typeof(response.error) !== 'undefined') { 
      alert('Your authentication Fail!'); 
      return false; 
     }; 
     return response; 
    }); 

}); 
相關問題