Angular 角度HTTP请求返回406

Angular 角度HTTP请求返回406,angular,interceptor,angular-httpclient,Angular,Interceptor,Angular Httpclient,我有一个HTTP请求的简单代码: intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { // add authorization header with jwt token if available request = this.addTokenToRequest(request); return next.handle(reque

我有一个HTTP请求的简单代码:

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  // add authorization header with jwt token if available
  request = this.addTokenToRequest(request);
  return next.handle(request)
  .pipe(
    catchError(error => {
      switch (error) {
              case 'Not Acceptable': {
                return this.handle406Error(request, next);
                break;
              }
              case 'Unauthorized': {
                // return this.handle401Error(request, next);
                break;
              }
        }
    }));

如果错误以字符串形式返回,http响应状态将为成功状态(2XX),此代码将不起作用,但需要使用rxjs的tap函数对响应部分进行检查,但如果不是,则下面的代码将对您起作用:

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      // add authorization header with jwt token if available
      request = this.addTokenToRequest(request);
      return next.handle(request)
      .pipe(
        catchError(error => {
     if(error instanceof HttpErrorResponse) {
          switch (error.status) {
                  case '406': {
                   console.log('HttpResponseError with status 406');
                   console.log(of(error));
                   return this.handle406Error(request, next);
                   break;
                  }
                  case '401': {
                    // return this.handle401Error(request, next);
                    console.log('HttpResponseError with status 401')
                    break;
                  }
           default: {
                    return of(error);
                    break;
                  }
            }
      else{
           console.log('My error is not an HttpErrorResponse');

           console.log(error);
             }
         }

        }));
    }
intercept(请求:HttpRequest,下一步:HttpHandler):可观察{
//添加带有jwt令牌的授权标头(如果可用)
请求=此.addTokenToRequest(请求);
返回next.handle(请求)
.烟斗(
catchError(错误=>{
if(HttpErrorResponse的错误实例){
开关(错误状态){
案例“406”:{
log(“状态为406的HttpResponseError”);
console.log(of(error));
返回此.handle406错误(请求,下一步);
打破
}
个案‘401’:{
//返回此.handle401错误(请求,下一步);
log('HttpResponseError,状态为401')
打破
}
默认值:{
返回(错误);
打破
}
}
否则{
log(“我的错误不是HttpErrorResponse”);
console.log(错误);
}
}
}));
}

不是解决方案,但我在这里要做的是添加一堆调试控制台日志,并检查值是什么。在您的情况下,您的错误显然是“OK”对的?您能详细说明一下吗?不确定我是否理解您的建议。您是否确认代码中的错误=“确定”?如果没有,我建议在switch语句之前执行“console.log(error)”,并在运行该请求时检查浏览器的控制台(我假设您在使用浏览器导航的web应用程序中使用此代码)。此外,您可以在案例中执行“console.log('random text')”,以确定触发了哪个案例。我在控制台中添加了一个屏幕截图。好的,我对此进行了深入研究,发现并浏览了一下,似乎“ok”是某种默认状态文本。您可以做什么,以确认其“实际OK响应”是否为console.log(error.OK),在您的情况下,它应该输出“false”。此外,您还可以使用console.log(error.status)检查状态代码。我建议您使用console.log来代替运行调试器,因为在我看来,调试器并没有将错误显示为对象,而只是显示它的字符串表示形式。是的,我可以尝试,但我不确定这是否会有帮助,因为“error instanceof HttpErrorResponse”将失败,因为我的错误对象只是一个值为“OK”的字符串。响应如下:“Unauthenticated Token”。很好,告诉我你只是返回一个简单的字符串?或者HttpErrorResponse?看起来就像一个字符串。我没有写后端代码。在这一点上,我所需要的就是能够识别406。我变得“不可接受”。不明白为什么现在是“OK”。您是否更改了您的帐户中令牌的过期时间?
 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      // add authorization header with jwt token if available
      request = this.addTokenToRequest(request);
      return next.handle(request)
      .pipe(
        catchError(error => {
     if(error instanceof HttpErrorResponse) {
          switch (error.status) {
                  case '406': {
                   console.log('HttpResponseError with status 406');
                   console.log(of(error));
                   return this.handle406Error(request, next);
                   break;
                  }
                  case '401': {
                    // return this.handle401Error(request, next);
                    console.log('HttpResponseError with status 401')
                    break;
                  }
           default: {
                    return of(error);
                    break;
                  }
            }
      else{
           console.log('My error is not an HttpErrorResponse');

           console.log(error);
             }
         }

        }));
    }