如何从 angular http 的响应中读取状态?

Ani*_*nil 4 httpresponse http-headers angular angular-httpclient

我在从响应中读取状态代码时遇到了一些问题。

我在服务中调用 api,

return this.http.get<string>( this.remoteServer + '/google.com/open', httpOptions);
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我有,

open() {
    this.openService.open(this.selected.qrCode)
    .subscribe(
      data => {
       console.log(data);
       this.toastService.showSuccess('Unlock Successfull', 'success');
      }
    );
  }
Run Code Online (Sandbox Code Playgroud)

现在我想阅读 http 状态文本,

我从上述调用中得到的示例 http 响应是。

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: " https://google.com/open ", ok: false, ...}

如何读取控制器中的状态文本。

请帮我

Ami*_*ani 6

您可以{ observe: 'response' }get请求指定为第二个参数,它为您提供完整的响应数据。

如果你想发送其他选项,headers或者params与它一起发送,只需以这种方式将所有选项汇总到一个对象中。

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json'}), params : myParams, observe: 'response'}
Run Code Online (Sandbox Code Playgroud)

参考

return this.http.get<string>(
    this.remoteServer + '/google.com/open', httpOptions).subscribe((data) => {
        console.log(data.status);     // staus 200
        console.log(data.statusText);  // OK
});
Run Code Online (Sandbox Code Playgroud)