![](/img/trans.png)
[英]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.