簡體   English   中英

Angular6 HttpInterceptor 獲取響應體

[英]Angular6 HttpInterceptor get response body

我正在嘗試在 Angular6 HttpInterceptor 中獲取純文本響應正文。 我的代碼是

export class HttpErrorHandlerService implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req)
      .pipe(catchError(err => {
        console.log('err body');
        console.log(err.error);
        return empty();
      }));      
  }
} 

此時的 err.error 是一個屬性類型為“text/plain”的 Blob。
我想獲取響應正文,以便我可以獲取所有錯誤消息並將它們放在一起。

看起來 Angular 使用返回字符串而不是 Blob,但是我現在還沒有找到如何做到這一點,因為它已經改變了。

有誰知道怎么獲得嗎?

你好,可以試試下面的代碼嗎

var reader = new FileReader();
reader.onload = function() {
    alert(reader.result);
}
reader.readAsText(err.error);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM