后端(netcore)返回500时,Angular中没有响应头

时间:2019-02-26 08:45:50

标签: angular asp.net-core cors

我有一个来自Angular的带有HttpIntercept的简单应用程序,当我从后端(在netcore 2.2中创建)收到InteralServerError(500)时,我试图访问/读取自定义标头

在netcore中,我允许CORS和所有方法一起使用

.UseCors(options =>
            {
                options.AllowAnyOrigin();
                options.AllowAnyMethod();
                options.AllowAnyHeader();
            })

在angular 7中,我使用的是Angular的HTTPIntercept,但是由于某些未知原因,如果我有500甚至400,我仍然无法从后端读取任何标头

角度拦截器

    @Injectable()
export class ErrorInterceptorService implements HttpInterceptor {
    ErrorRouterName = 'error';

    constructor(
        private router: Router,
        private coreService: CoreService,
    ) { }

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


        console.log('this is something --->> ', req.headers.keys());
        return next.handle(req).pipe(
            map((event: any) => {
                if (event instanceof HttpResponse) {
                    // this.errorDialogService.openDialog(event);
                    console.log(event.headers.getAll('x-correlation-id'));
                    console.log(event.headers.getAll('X-Correlation-ID'));
                }
                if (event instanceof HttpHeaderResponse) {
                    console.log(event.headers.getAll('x-correlation-id'));
                    console.log(event.headers.getAll('X-Correlation-ID'));
                }
                if (event instanceof HttpRequest) {
                    console.log('event--->>>', event);
                    console.log(event.headers.keys());
                    // this.errorDialogService.openDialog(event);
                    console.log(event.headers.getAll('x-correlation-id'));
                    console.log(event.headers.getAll('X-Correlation-ID'));
                }
                if (event instanceof HttpResponseBase) {
                    console.log('eventerror--->>>', event);
                    console.log(event.headers.keys());
                    // this.errorDialogService.openDialog(event);
                    console.log(event.headers.getAll('x-correlation-id'));
                    console.log(event.headers.getAll('X-Correlation-ID'));
                }

                console.log(event);
                return event;
            }),
            catchError((error: HttpEvent<any>) => {
                console.log(error);
                return throwError(error);
            }));
    }
}

一个有趣的事实是,在邮递员中,我也可以在浏览器(Chrome)中看到自定义标头,但在Angular中除外

我做错了什么?我错过了什么 ?

我也从这里尝试过这种方法: https://scotch.io/@vigneshsithirai/angular-6-7-http-client-interceptor-with-error-handling

并创建了一个空白新项目,启用了CORS,但仍然没有成功

0 个答案:

没有答案