gpt4 book ai didi

angular - 获取状态码 http.get response angular2

转载 作者:太空狗 更新时间:2023-10-29 17:08:16 29 4
gpt4 key购买 nike

我需要获取以下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);

}

谢谢!

最佳答案

为 Angular >= 4.3(包括 11)版本添加新的 HttpClient 替换 http

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 response angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683052/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com