[Angular] Read Custom HTTP Headers Sent by the Server in Angular
2023-09-14 08:59:17 时间
By default the response body doesn’t contain all the data that might be needed in your app. Your server might return some special header which you have to read explicitly. In such case we can use the { observe: ‘response’}
configuration of the Angular HttpClient
. Let’s explore how.
import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { HttpClient, HttpResponse } from '@angular/common/http'; export interface Person { name: string; } @Injectable() export class PeopleService { constructor(private http: HttpClient) {} fetchPeople(): Observable<HttpResponse<Person>> { return this.http .get<Person>('data/people.json', { observe: 'response'}); } }
Now instead of just returning your data, it returns your response object.
{ "headers": { "normalizedNames": [], "lazyUpdate": null }, "status": 200, "statusText": "OK", "url": "https://run.plnkr.co/preview/cjdn2x8fh000ffillqi8d3o4k/data/people.json", "ok": true, "type": 4, "body": [ { "name": "xxx" }, { "name": "xxx" } ] }
相关文章
- 模块化开发 Angular 应用 [含懒加载]
- angular面试问题_kafka面试题
- orbital angular momentum_omnidirectional
- Angular Feature Modules
- Angular 项目多国语言设置
- Angular 应用里 server.ts 文件的 APP_BASE_HREF token 的用法?
- ORA-26859: Server “string” is not an XStream outbound server ORACLE 报错 故障修复 远程处理
- MySQL Error number: 3739; Symbol: ER_SRS_INVALID_ANGULAR_UNIT; SQLSTATE: SR002 报错 故障修复 远程处理
- 和 sql server支持两者:Oracle 和 SQL Server的兼容性(兼容oracle)
- 使用angular写一个helloworld