Angular2 http post获取请求的正文

时间:2017-11-27 07:28:21

标签: angular typescript http-post angular2-services angular2-http

我想知道如何在Angulars2中获取HTTP POST请求的正文。 当我回到错误方法时,身体是可访问的。

return this.http.post(requestUrl, **body**, options) 
.map((res:Response) => res.json(),
    (err)=> {
        return {"errorObj":err, "requestBody":**body**}
    } )

感谢。

1 个答案:

答案 0 :(得分:0)

你可以测试一下。工作。会给你身体要求和网址。

this.http.post(url, body, this.options).subscribe(
  data => {
    console.log('ok url:' + url);
    console.log('body:' +  JSON.stringify(body));
  },
  (err: HttpErrorResponse) => {
    if (err.error instanceof Error) {
      // A client-side or network error occurred. Handle it accordingly.
      console.log('instanceof error url:' + url);
      console.log('body:' + JSON.stringify(body));
      console.log('An error occurred:', err.error.message);
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      console.log('backend error url:' + url:' + url);
      console.log('body:' + JSON.stringify(body));
      let strBody = JSON.stringify(err);
      strBody = JSON.stringify(err.error);
      strBody = JSON.stringify(err.status);
      console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
    }
  }
);