如何从打字稿中获取http响应

时间:2019-08-16 11:40:09

标签: angular typescript http post

我尝试查看其他问题,但它们似乎无济于事。我在打字稿中有以下方法

transferAmount(transfer: Transfer): Observable<number> {
return this.http
  .post<number>(
    `${this._baseUrl}:8092/api/transfers/`,
    transfer,
    { observe: "response" }
  )
  .pipe(
    mergeMap((response: any) => {
      console.log(response.json());
      const location = `${response.headers.get("Location")}`;
      console.log(parseInt(location.slice(location.lastIndexOf("/") + 1)))
      return of(parseInt(location.slice(location.lastIndexOf("/") + 1)));
    })
  );

}

我尝试使用

记录响应
console.log(response.json()) 

但这不起作用吗?

2 个答案:

答案 0 :(得分:0)

订阅这样的可观察对象可以得到答案:

transferAmount(..).subscribe(res => console.log(res));

答案 1 :(得分:0)

尝试以下操作:

componentDidMount

当您执行请求时,post方法将向您返回一个可观察的对象,但是您需要订阅该可观察的对象以获取响应。