簡體   English   中英

如何從打字稿中獲取http響應

[英]how to get http response from typescript 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()) 

但這行不通嗎?

訂閱可觀察對象時,您可以得到答案:

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

試試這個:

this.transferAmount(transfer: Transfer).subscribe(resp => JSON.stringfy(resp));

當您發出請求時,post方法將向您返回一個可觀察對象,但是您需要訂閱該可觀察對象以獲取響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM