【问题标题】:Angular2 http post get the body of the requestAngular2 http post获取请求的正文
【发布时间】:2018-05-10 09:17:20
【问题描述】:

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

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

谢谢。

【问题讨论】:

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


    【解决方案1】:

    你可以测试一下。在职的。会给你正文请求和网址。

    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}`);
        }
      }
    );
    

    【讨论】:

      猜你喜欢
      • 2017-10-07
      • 2015-02-18
      • 1970-01-01
      • 2016-05-14
      • 2017-03-12
      • 1970-01-01
      • 2017-03-10
      • 2011-05-27
      • 2015-07-10
      相关资源
      最近更新 更多