如果HTTP请求有err_connection_refused
错误,我想向用户显示一条消息。我收到此错误,因为服务器已断开连接。
http.get().subscribe(
(response) => {
},
error => {
// some code to know error is err_connection_refused
}
);
这是你想要的方法。只需查看控制台中的错误对象,看看拒绝连接的状态:
http.get().pipe(
// here we can stop the error being thrown for certain error responses
catchError(err: HttpErrorResponse => {
console.log(err)
if (err.status == 404) return of(null)
else throwError(err)
})
).subscribe(
(response) => {
},
// if the error is thrown (or not caught if you do not use catchError) we hit this block
error => {
console.log(err.status)
// some code to know error is err_connection_refused
}
);