如何在状态401上禁用错误功能

时间:2018-11-04 10:25:59

标签: javascript angular web httpclient unauthorized

您好,我尝试使用401(未经授权)和403(禁止)httprequests拦截器,并且在禁用401错误时禁用错误功能存在问题。

拦截器:

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http'
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req).do(event => {}, err => {
            if (err instanceof HttpErrorResponse && err.status == 401) {
                // alert unauthorized & redirect to login.
            }
        });
    }
}

请求:

request(data){
    this.httpservice.request(data).subscribe(
      res => alert("success"),
      err => {
          /* doing something very complex that unauthorized
          users should not process to */
      }
    );
}

因此,在给定的示例中,拦截器代码(重定向)和请求err处理功能都在运行。 如果服务器发出401结果,我想禁用err结果功能。 不幸的是,我找不到办法。

0 个答案:

没有答案