0
我有我想如果響應是401,403,還是500像那麼回事可觀察到的服務:捕不捕誤差角2
post(url, data) {
let headers = new Headers();
headers = this.createAuthorizationHeader(headers);
return this.http.post(url, data, {
headers: headers
}).catch(this.catchAuthError(this));
}
catchAuthError方法:
private catchAuthError (self: HttpUtil) {
// we have to pass HttpService's own instance here as `self`
return (res: Response) => {
if (res.status === 401 || res.status === 403 || res.status === 500) {
//TODO: route the user to the login page again and make a 500 ERROR page
// if not authenticated
console.log("ERROR: "+res);
this.relogin.showDialog();
}
return Observable.throw('Not authenticated: '+res);
};
}
我不知道爲什麼該方法沒有被觸發,但網絡選項卡(chrome)自然會告訴我我得到了哪個錯誤。但是控制檯未顯示console.log("ERROR: "+res);
AFAIK傳遞給'捕獲的錯誤處理程序(......)'會得到錯誤傳遞,而不是'Response'。你檢查過了嗎?你也不需要傳遞'this' /'self',如果你使用箭頭函數'this'將繼續在'catchAuthError'中工作(比如'})catch((e)=> this.catchAuthError(e) );' –