Angular HttpClient - 获取响应中的头信息

4

我知道有很多已经回答过的问题,但是没有一个适用于我。

我正在尝试进行以下调用:

this.http.options("http://...", {observe: 'response'}).subscribe((data: HttpResponse<any>) => console.log(data))

然而,该输入的头字段为空!:
HttpResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: 

"http://...", ok: true, }
body: ""
headers: HttpHeaders
lazyInit: ƒ ()
lazyUpdate: null
normalizedNames: Map(0)
size: (...)
__proto__: Map
[[Entries]]: Array(0)
length: 0
__proto__: Object
ok: true
status: 200
statusText: "OK"
type: 4
url: "http:/..."
__proto__: HttpResponseBase

但是如果我使用Postman进行相同的调用,我会得到下列标头!

Allow →PUT, HEAD, OPTIONS, GET
Content-Length →0
Content-Type →text/html; charset=utf-8
Date →Fri, 28 Sep 2018 21:29:22 GMT
Server →Google Frontend
X-Cloud-Trace-Context →6627df99d34998ddeadcdcf7a2b132be;o=1

我需要获取“允许”标题,但似乎无法做到。

请帮助。


请查看此链接 https://dev59.com/wqvka4cB1Zd3GeqPnRBp#50135330 - Fateh Mohamed
1个回答

6

我看到你的问题有两个问题:

  1. Using Angular's HttpClient, response headers are lazy-loaded, which means you need to attempt to get the value in order to actually see that it exists. e.g.:

     const allowValue = response.headers.get('allow');
    
  2. To allow your client-side JavaScript to access this header, you need to set a response header (server-side, of course). The following line shows the header and the value that needs to be set:

     Access-Control-Expose-Headers: allow
    
您可以在 MDN 上阅读更多关于此的信息:Access-Control-Expose-Headers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接