我们可以在Angular的HTTP拦截器中处理这个问题。具体步骤如下:
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class InterceptorService implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest, next: HttpHandler): Observable {
return next.handle(request).pipe(
catchError((err: HttpErrorResponse) => {
if (err.status === 200) {
return next.handle(request.clone({ headers: request.headers.set('Content-length', '0') }));
} else {
return throwError(err);
}
})
)
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppComponent } from './app.component';
import { InterceptorService } from './interceptor.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: InterceptorService,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
这样就解决了响应代码为200被视为错误的问题。