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

落爺英雄遲暮 提交于 2019-12-22 08:38:20

问题


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.

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.


回答1:


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.




回答2:


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
});


来源:https://stackoverflow.com/questions/40471271/cant-get-all-response-headers-from-angular-2-with-cors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!