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

标签 angular http-headers httpresponse angular-httpclient

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

我正在服务中调用api,

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

在我的 Controller 中,

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

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

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

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

如何读取 Controller 中的状态文本。

请帮助我

最佳答案

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

如果您想连同 headersparams 一起发送其他选项,只需以这种方式将所有它们汇总到一个对象中即可。

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

Reference

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

关于angular - 如何从 Angular http 的响应中读取状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51049806/

相关文章:

mobile - 如何在Web服务器上检测移动客户端?

安卓 : Capturing HTTP Requests with non-rooted android device

spring-mvc - 如何在 Spring MVC 中代理 HTTP 请求?

Angular2 升级到 RC6,找不到 traceur

angular - NgbDatePicker - 如何将 longDate 字符串绑定(bind)到 [ngModel]?

java - 什么是可以拆分 http header 列表的最佳正则表达式?

c# - HttpResponse.Filter 如何确定流结束

Django:重定向到一个页面,然后再次重定向回来?

angular - 使用Angular 2启动虚拟机

javascript - iFrame 关闭时 session 存储不会重置