簡體   English   中英

Angular HttpClient - 從響應中獲取標頭

[英]Angular HttpClient - Get headers from response

我知道對此有很多回答的問題,但沒有一個對我有用。

我正在嘗試進行以下調用:

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

我需要獲得“允許”標題,但我似乎無法做到。

請幫助

我在你的問題中看到兩個問題:

  1. 使用 Angular 的HttpClient ,響應頭是延遲加載的,這意味着您需要嘗試get該值才能實際看到它存在。 例如:

     const allowValue = response.headers.get('allow');
  2. 要允許您的客戶端 JavaScript 訪問此標頭,您需要設置一個響應標頭(當然是服務器端)。 以下行顯示了需要設置的標題和值:

     Access-Control-Expose-Headers: allow

您可以在 MDN 上閱讀更多相關信息: Access-Control-Expose-Headers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM