我正在製作一個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
}
}
}]);
顯示你的代碼隊友 – Satpal
@Satpal請檢查 – pranavjindal999