Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get all response headers from angular 2 with cors

Tags:

cors

angular

I have an Angular 2 (2.1.2) client, ASP.NET Core as a backend with CORS enabled. Normal APIs (GET, POST, DELETE, ...) is working fine, my problem is when I try to retrieve headers from the response; particularly Content-Disposition. Here is a screen shot when I try to get a file content using Ajax.

OPTIONS request

GET request

As you can see there is a header called Content-Disposition in the GET response headers, but it is missing when I try to retrieve it from the Angular http service.

A quick note, I am only using CORS in development.

like image 555
Ayman Avatar asked Nov 07 '16 17:11

Ayman


2 Answers

You need to configure the server so that it adds the response header

access-control-expose-headers: content-disposition

otherwise the browser won't make the header available to JavaScript.

like image 180
Günter Zöchbauer Avatar answered Nov 11 '22 13:11

Günter Zöchbauer


To be even more precise, in ASP.NET Core, you can configure it like this in your Startup class, in the Configure method:

app.UseCors(x =>
{
     x.WithExposedHeaders("Content-Disposition");
     // .. omitted
});
like image 7
mode777 Avatar answered Nov 11 '22 13:11

mode777