从Angular 10调用rest api时无法获得正确的错误状态

问题描述

我无法正确使用Httpinterceptor,因为我没有获得正确的401错误代码。 我的代码是:-

export class RefreshTokenInterceptor implements HttpInterceptor {


  private isRefreshing = false;
  private refreshTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(null);

  constructor(public refreshService: RefreshService) { }

  intercept(request: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {

    if (this.refreshService.getJwtToken()) {
      request = this.addToken(request,this.refreshService.getJwtToken());
    }

    return next.handle(request).pipe(catchError(error => {
      
      if (error instanceof HttpErrorResponse && error.status === 401) {
        return this.handle401Error(request,next);
      } else {
        return throwError(error);
      }
    }));
  }

但是我在 error.status 中得到0。下面提到我在Angular中遇到的错误

HttpErrorResponse {headers: HttpHeaders,status: 0,statusText: "UnkNown Error",url: "http://localhost:8084/api/v1/category",ok: false,…}
error: ProgressEvent {isTrusted: true,lengthComputable: false,loaded: 0,total: 0,type: "error",…}
headers: HttpHeaders {normalizednames: Map(0),lazyUpdate: null,headers: Map(0)}
message: "Http failure response for http://localhost:8084/api/v1/category: 0 UnkNown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "UnkNown Error"
url: "http://localhost:8084/api/v1/category"
__proto__: HttpResponseBase

从Rest Client呼叫时。它显示状态401 。响应如下:

Rest Client

请帮助我,以解决此问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...