获取状态码 http.get 响应 angular2 [英] Get status code http.get response angular2
本文介绍了获取状态码 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);
}
谢谢!
推荐答案
使用新的 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屋!
查看全文