簡體   English   中英

Angular 8 HttpClient GET 響應不返回自定義標頭

[英]Angular 8 HttpClient GET response not returning the custom headers

我試圖在 http 請求后獲取響應標頭,但我無法獲取它...

headers: HttpHeaders;
httpOptions;

  constructor(private http: HttpClient) {
    const token = localStorage.getItem("token");
    this.headers = new HttpHeaders({
      "Content-Type": "application/x-www-form-urlencoded"
    }).set("x-auth-token", token);

    this.httpOptions = {
    headers: this.headers,
    responseType: "text" as "json",
    observe: "response"
};}


test<T>(url: string) {
    return this.http.get<T>(url, this.httpOptions).pipe(
      tap((res: HttpResponse<any>) => {
        console.log(res.headers.keys())
      }))
  }

我試圖得到這個:標題

看看這個:獲得完全響應

我在這里嘗試了這段代碼,它有效:

    this.http.get(this.url, { observe: `response` })
    .subscribe((res) => {
      console.log(res);
    })

我使用訂閱而不是管道。 這是回應:

控制台上打印的響應

我不確定這是否有幫助。

暫無
暫無

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

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