【问题标题】:Angular 4 - interceptor handle net::ERR_CONNECTION_REFUSEDAngular 4 - 拦截器句柄 net::ERR_CONNECTION_REFUSED
【发布时间】:2017-11-16 20:42:36
【问题描述】:

在下面的代码中,我尝试拦截我的 http 请求。每当我可以建立一个http连接时,我的拦截器就会工作,但是当我得到net::ERR_CONNECTION_REFUSED时,event变量不是HttpResponse的实例,所以我无法处理这种错误。这是我的代码:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.changeStatus(false);
    this.requests_count++;
    const my_req = req.clone({
        url: API_PATH + req.url
    });
    return next
        .handle(my_req)
        .do(event => {
            if (event instanceof HttpResponse) {
                this.requests_count--;
                if (!this.requests_count)
                    this.changeStatus(true);
            }
        });
}

如何在拦截器中检测到 net::ERR_CONNECTION_REFUSED 之类的错误?

【问题讨论】:

    标签: angular angular-http-interceptors


    【解决方案1】:

    刚刚收到!

    .do(
        event => {
            if (event instanceof HttpResponse) {
                this.requests_count--;
                if (!this.requests_count)
                    this.changeStatus(true);
            }
        },
        error => {
            if (error instanceof HttpErrorResponse) {
                this.requests_count--;
                if (!this.requests_count)
                    this.changeStatus(true);
            }
        }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-07
      • 2018-05-19
      • 2017-06-15
      • 2018-07-08
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      相关资源
      最近更新 更多