angular获取请求头_Angular 4.3.3 HttpClient:如何从响应的标头获取值?

这篇博客讨论了在Angular 4.3.3中使用HttpClient时如何正确获取请求响应头的问题。作者遇到了Typescript错误,尝试了不同的选项设置,并最终找到了一个可行的解决方案。错误源于将'observe'设为字符串而不是预期的'body'或'response'。通过设置'observe'为'response'和'responseType'为'json',作者能够成功获取完整响应,包括标头。
摘要由CSDN通过智能技术生成

(编辑:VS代码;打字稿:2.2.1)

目的是获取请求响应的标头

假设服务中具有HttpClient的POST请求

import {

Injectable

} from "@angular/core";

import {

HttpClient,

HttpHeaders,

} from "@angular/common/http";

@Injectable()

export class MyHttpClientService {

const url = 'url';

const body = {

body: 'the body'

};

const headers = 'headers made with HttpHeaders';

const options = {

headers: headers,

observe: "response", // to display the full response

responseType: "json"

};

return this.http.post(sessionUrl, body, options)

.subscribe(response => {

console.log(response);

return response;

}, err => {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值