2017-05-05 88 views
0

我正在製作一個http攔截器,並且我想從response$httpProvider攔截器中拋出錯誤。

按照文檔:

response : interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object.

responseError : interceptor gets called when a previous interceptor threw an error or resolved with a rejection.

我想使之與狀態200的響應(我有一個條件)被路由到responseError,我將處理做在上述報價的加粗部分錯誤。 不返回響應拋出以下錯誤:

Cannot read property 'data' of undefined 

我不想返回響應,但想要將它傳遞給下一個處理即responseError

我該怎麼做? 我希望我說清楚。 謝謝。

更新(下面的代碼):

app.config(['$httpProvider', function($httpProvider) { 
    interceptor.$inject = ['$q', '$rootScope']; 
    $httpProvider.interceptors.push(interceptor); 

    function interceptor($q, $rootScope) { 
     return { 
      response: response, 
      responseError: responseError 
     }; 

     function response(response) { 
      if (response.data.rs == "F") { 
       // I want to route it responseError --> 
      } else { 
       return response; 
      } 
     } 

     function responseError(response) { 
      // I want to handle that error here 
     } 
    } 

}]); 
+0

顯示你的代碼隊友 – Satpal

+0

@Satpal請檢查 – pranavjindal999

回答

1

用途:

return $q.reject(response); 

另外,還要確保你返回:(閱讀關於它here

return response || $q.when(response);

,而不是:

return response; 
+0

我試過這個'return $ q.reject(response);'但是控件不會去'responseError' ..: – pranavjindal999