如何在Angular 7中处理`err_connection_refused` HTTP错误?

问题描述 投票:0回答:1

如果HTTP请求有err_connection_refused错误,我想向用户显示一条消息。我收到此错误,因为服务器已断开连接。

http.get().subscribe(
    (response) => {

    },
    error => {
        // some  code to know error is err_connection_refused
    }
);
angular
1个回答
0
投票

这是你想要的方法。只需查看控制台中的错误对象,看看拒绝连接的状态:

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
    }
);
© www.soinside.com 2019 - 2024. All rights reserved.