不能看到http帖子的标题[Angular4] [英] Cant see headers of http post [Angular4]

查看:93
本文介绍了不能看到http帖子的标题[Angular4]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易解决,但是我知道我找不到解决办法.

This is something that should be easy to fix but right know my mind can't find a solution.

我有这样的帖子:

getAccordo(filtro: Filter): Observable<Response>  {
    return this.http.post<Response>(`${this.ENDPOINT}/export`,
        filtro,
        {observe: 'response', responseType: 'blob' as 'json'}   
    )
        .catch(error => this.handleError(error));
}

这是我使用它的地方:

  public getAccordo(event: any): any {debugger;
    event.preventDefault();
    this.accordiService.getAccordo(this.filter).subscribe(
      res => {debugger;
        const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
        const anchor = document.createElement('a');
        anchor.href = window.URL.createObjectURL(blob);
        anchor.download = Utils.extractHeaders(res);
        anchor.click();
      },
      error => {
        console.log(error);
      }
    );
  }

当我查看chrome控制台时,可以看到标题中有一个content-disposition: attachment; filename="esportazione_accordi_20180905.xlsx"

When I look the chrome console I can see that in the headers there is a content-disposition: attachment; filename="esportazione_accordi_20180905.xlsx"

但是在我的'res'中,这不见了.

But in my 'res' this is missing.

id想要做的是获取文件名并在anchor.download中使用它.

What id like to do is to get the filename and use it in anchor.download.

如果您需要其他帮助,谢谢您.

Thank you for the help if you need something more just ask.

编辑

这是我从响应标头中获得的标头,我什至看不到其中的1个:

This is the headers that i get from the Response Headers, I dont see even 1 of them:

在服务器端我添加了.header("Access-Control-Expose-Headers", "content-disposition")但仍然无法正常工作

On the server side ive added: .header("Access-Control-Expose-Headers", "content-disposition") But still it doesnt work

好的,我们越来越近了,我需要在图片末尾获取文件名"值,并将其分配给anchor.download.

Ok we are getting closer, I need to get the "filename" value at the end of the pic and assign that to anchor.download.

推荐答案

为了将此头文件公开到Angular XHR,服务器将需要设置Access-Control-Expose-Headers响应头,以允许专门访问Content-Disposition头. 文档有更多内容有关该主题的信息:

In order to expose this header to your Angular XHR, the server will need to set the Access-Control-Expose-Headers response header to allow specifically access to the Content-Disposition header. The docs have more information on the subject:

默认情况下,仅公开6个简单的响应标头:

By default, only the 6 simple response headers are exposed:

  • 缓存控制
  • 内容语言
  • 内容类型
  • 到期
  • 最后修改
  • 编译指示
  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

如果希望客户端能够访问其他标头,则必须使用Access-Control-Expose-Headers标头列出它们.

If you want clients to be able to access other headers, you have to list them using the Access-Control-Expose-Headers header.

这意味着您的服务器需要设置以下标头:

This means that your server needs to set the following header:

Access-Control-Expose-Headers: Content-Disposition

在Angular中,使用HttpClient,响应标​​头被延迟解析-要获取特定标头的值,您将需要使用以下方法来标明get标头:

In Angular, using HttpClient, the response headers are lazily parsed - To get a specific header's value, you'll need to to get the header using something like this:

res.headers.get('Content-Disposition');

这篇关于不能看到http帖子的标题[Angular4]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆