[英]Angular HttpClient make GET request with JSON in the body
[英]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.