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

//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);

}

谢谢!

最佳答案

使用替代HttpClient的新http为Angular> = 4.3(包括10)版本添加答案

import {HttpClientModule} from '@angular/common/http'; // Notice it is imported from @angular/common/http instead of @angular/http
如何获取响应代码或任何其他 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的评论所述,
对象{observe: 'response'}是使完整响应对象可用的原因。
Check Documentation

关于angular - 获取状态码http.get响应angular2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43683052/

10-16 15:50
查看更多