获取状态码http.get响应angular2 [英] Get status code http.get response angular2

查看:191
本文介绍了获取状态码http.get响应angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取以下http调用的状态代码,并将其作为字符串返回

I need to get the status code of the following http call and return it as a string

//This method must return the status of the http response
confirmEmail(mailToken):Observable<String>{

     return this.http.get(this.baseUrl+"users/activate?mailToken="+mailToken)
                     .map(this.extractData)
                     .catch(this.handleError);

}

thx!

推荐答案

使用新的HttpClient替换http

import {HttpClientModule} from '@angular/common/http'; // Notice it is imported from @angular/common/http instead of @angular/http

如何获取响应代码或任何其他标头:

How to get response code or any other header:

http.get(
   `${this.baseUrl}users/activate?mailToken=${mailToken}`,
    {observe: 'response'}
)
  .subscribe(response => {

    // You can access status:
    console.log(response.status);

    // Or any other header:
    console.log(response.headers.get('X-Custom-Header'));
  });

如@Rbk的评论所述,

As noted in the comment by @Rbk,

对象{observe: 'response'}是使完整响应对象可用的原因.

The object {observe: 'response'} is what makes the full response object available.

检查文档

这篇关于获取状态码http.get响应angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆