[英]Angular HttpClient - Get headers from response
I know there are many answered questions to this but none of them has worked to me.我知道对此有很多回答的问题,但没有一个对我有用。
I am trying to do the following call:我正在尝试进行以下调用:
this.http.options("http://...", {observe: 'response'}).subscribe((data: HttpResponse<any>) => console.log(data))
However the headers field from that input are empty!但是,该输入的标题字段是空的! :
:
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
But if I do that same call with Postman I get the following headers!但是,如果我与 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
I need to get the 'Allow' headers but I don't seem to be able to do it.我需要获得“允许”标题,但我似乎无法做到。
Help pls请帮助
I see two problems in your question:我在你的问题中看到两个问题:
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.使用 Angular 的
HttpClient
,响应头是延迟加载的,这意味着您需要尝试get
该值才能实际看到它存在。 eg:例如:
const allowValue = response.headers.get('allow');
To allow your client-side JavaScript to access this header, you need to set a response header (server-side, of course).要允许您的客户端 JavaScript 访问此标头,您需要设置一个响应标头(当然是服务器端)。 The following line shows the header and the value that needs to be set:
以下行显示了需要设置的标题和值:
Access-Control-Expose-Headers: allow
You can read more about this on MDN: Access-Control-Expose-Headers .您可以在 MDN 上阅读更多相关信息: Access-Control-Expose-Headers 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.