gpt4 book ai didi

angular - 如何以 Angular 获取HttpClient模块的状态码

转载 作者:行者123 更新时间:2023-12-05 02:37:49 27 4
gpt4 key购买 nike

如何使用HttpClient获取所有请求的状态码

apiCall() {
let header1 = new HttpHeaders();
header1 = header1.append('Content-Type', 'application/json');
header1 = header1.append('Authorization', `Bearer ${this.token}`);

this.http.post("URL",
{
"dataSentHere": "data"
}, {
headers: header1
}).subscribe(data => {

console.log(data);

},
(err: HttpErrorResponse) => {
if (err.status === 403) {
console.log("403 status code caught");
}
}


)
}

如何获取返回202、200等的http请求的状态码

最佳答案

使用 '@angular/common/http' 中的 HttpErrorResponseHttpResponse

apiCall() {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json');
headers = headers.set('Authorization', `Bearer ${this.token}`);

this.http.post<HttpResponse<any>>('URL', { data }, { headers }).subscribe(response => {
console.log(response.status) // log status code
}, (e: HttpErrorResponse) => console.log(e.status))

}

关于angular - 如何以 Angular 获取HttpClient模块的状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69898556/

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