簡體   English   中英

角4 httpclient獲取錯誤后的請求正文

[英]angular 4 httpclient get body of post request on error

我正在嘗試找到一個示例,該示例在發生錯誤的情況下如何獲取POST請求的主體(下面的queryObj),但我找不到一個:

this.http.post<ApiResponse>(
        url,
        queryObj,
        { headers: new HttpHeaders(headers), observe: 'response' }
    ).subscribe(
       resp => { ... },
       err => { // get queryObj }
    )

謝謝。

您已經有了queryObj對象,它是您要發送的對象,在訂閱的error部分,您從郵政服務中獲取了異常,如果您的請求不成功,它應該引發,在這種情況下,只顯示queryObj和像這樣的一些錯誤:

//當然要事先定義this.error和this.response

this.http.post(url, queryObj, { headers: new HttpHeaders(headers) })
    .subscribe(
       resp => { 
          this.response = resp;
          // ... whatever action you wanna perform here
       },
       err => { 
          this.error = err;
          console.log("Request failed due to " + this.error);
          console.log("Body of the post request: ");
          console.log(queryObj);
       });

暫無
暫無

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

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