簡體   English   中英

使用 httpClient.get() 從 angular 獲取標頭

[英]get headers from angular with httpClient.get()

郵遞員回復

200 還好

身體:

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Workspace</title>
    <base href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>

<body>
    <app-root></app-root>
    <script src="runtime.js" type="module"></script>
    <script src="polyfills.js" type="module"></script>
    <script src="styles.js" type="module"></script>
    <script src="vendor.js" type="module"></script>
    <script src="main.js" type="module"></script>
</body>

</html>

有角度的

getParamsFromHeaders(): Observable<any> {

  return this.http.get<any>('http://localhost:4200/').pipe(
    catchError(err => {

      return throwError(err);

  })
);

}

響應角

錯誤,因為我是一個 html 內容......我可以訪問標題。

有沒有辦法消除錯誤?

問候

您需要觀察整個響應,如下所述:

這是您的進口:-


import { HttpClient, HttpResponse } from '@angular/common/http';

import { tap } from 'rxjs/operators';

這是您在服務中使用的功能。


getParamsFromHeaders() : Observable<HttpResponse<Object>>{

   return this.http.get('http://localhost:4200/', { observe: 'response' }).pipe
(tap(resp => console.log('heaeder', r`enter code here`esp.headers.getAll('content-type')))
    );
}

暫無
暫無

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

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