我写了以下代码,当我在浏览器中运行代码时,登录api给出了403响应代码。但是我无法捕捉到该错误并进行控制台记录。谁能告诉我我的代码出了什么问题。
this.http
.post("login", {
username,
password
}).pipe(
catchError(err => {
console.log('Handling error locally and rethrowing it...', err);
return throwError(err);
})
)
.subscribe(
(response: any) => {
}
error=> {
console.log(error);
}
)
答案 0 :(得分:0)
尝试以下类似操作:
let params = {
"login_id" : id,
"password" : pass
};
this.http.post(loginUrl, params, {observe:'response'}).subscribe((data:any) => {
console.log('http login -> ', data);
}, (err : HttpErrorResponse) => {
console.log('http login error -> ', err);
});
}
或者也尝试一下:
this.http.post("login", {
username,
password
})
.pipe(catchError(this.errorMgmt))
.subscribe(
(response: any) => {
console.log('res',response)
}
)
errorMgmt(error: HttpErrorResponse) {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// Get client-side error
errorMessage = error.error.message;
console.log('err',errorMessage);
} else {
// Get server-side error
errorMessage = `Error Code: ${error.status}\nMessage: ${JSON.stringify(error.error)}`;
console.log('err',errorMessage);
}
console.log('errorMgmt -> ',(errorMessage));
return throwError(errorMessage);
}