2017-09-22 133 views
1

我正在用Angular HttpClient發出一個http請求。如何通過Angular 4獲得完整的錯誤響應?

http.get(`${hostUrl}/myproject/infrastructure-status`) 
.subscribe(
(resp) => { 
    // handle response 
}, 
(error) => { 
console.log(error) 
}); 

在我的控制檯,類型HttpErrorResponse的錯誤會記錄: Error logged on console

然而,我的網絡選項卡上的後端響應是不一樣的:

Headers error network tab

enter image description here

我可以在Angular中獲得完整的錯誤響應嗎?如果是這樣,怎麼樣?

回答

1

你得到的錯誤也是一個響應對象,所以如果你將它轉換爲響應,你可以閱讀正文。 對於我這樣工作的:

http.get(`${hostUrl}/myproject/infrastructure-status`) 
      .subscribe(
      (resp) => { 
       // handle response 
      }, 
      (error) => { 
       if (error instanceof Response) { 
       console.log(error.text()); 
       } 
      }); 

什麼覺得奇怪,我是,你得到一個ProgressEventObject。我總是得到一個包含body的Response對象。也許不同的是,你的情況下的響應是HTML,我正在測試調用返回JSON的WebApi。

+0

ProgressEvent可以通過標誌獲得。 – Zlatko

相關問題