程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular 4.3.3 HttpClient:如何从响应的标头获取值?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Angular 4.3.3 httpClient:如何从响应的标头获取值??

开发过程中遇到Angular 4.3.3 httpClient:如何从响应的标头获取值?的问题如何解决?下面主要结合日常开发的经验,给出你关于Angular 4.3.3 httpClient:如何从响应的标头获取值?的解决方法建议,希望对你解决Angular 4.3.3 httpClient:如何从响应的标头获取值?有所启发或帮助;

您可以观察到完整的响应,而不仅仅是内容。为此,您必须传递observe: responseoptions函数调用的参数中。

http
  .get<MyJsonData>('/data.Json', {observe: 'response'})
  .subscribe(resp => {
    // Here, resp is of type httpResponse<MyJsonData>.
    // You can inspect its headers:
    console.log(resp.headers.get('X-Custom-header'));
    // And access the body directly, which is typed as MyJsonData as requested.
    console.log(resp.body.someFIEld);
  });

解决方法

(编辑: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 => {
            throw err;
        });
}

第一个问题是我遇到Typescript错误:

'Argument of type '{ 
    headers: httpHeaders; 
    observe: String; 
    responseType: String;
}' is not assignable to parameter of type'{ 
    headers?: httpHeaders;
    observe?: "body";
    params?: httpParams; reportProgress?: Boolean;
    respons...'.

Types of property 'observe' are incompatible.
Type 'String' is not assignable to type '"body"'.'
at: '51,49' source: 'ts'

确实,当我转到post()方法的ref时,我指出了这个原型(我使用VS代码)

post(url: String,body: any | null,options: {
        headers?: httpHeaders;
        observe?: 'body';
        params?: httpParams;
        reportProgress?: Boolean;
        responseType: 'arraybuffer';
        withCredentials?: Boolean;
    }): Observable<ArrayBuffer>;

但是我想要这个重载的方法:

post(url: String,options: {
    headers?: httpHeaders;
    observe: 'response';
    params?: httpParams;
    reportProgress?: Boolean;
    responseType?: 'json';
    withCredentials?: Boolean;
}): Observable<httpResponse<Object>>;

因此,我尝试使用此结构修复此错误:

  const options = {
            headers: headers,"observe?": "response","responseType?": "json",};

并编译!但是我只是得到json格式的主体请求。

此外,为什么我必须放一个?字段名称末尾的符号?正如我在Typescript网站上看​​到的那样,该符号应该只是告诉用户它是可选的?

我也尝试使用所有字段,不带和带?分数

编辑

this.http.post(url).map(resp => console.log(resp));

Typescript编译器告诉您映射不存在,因为它不是Observable的一部分

我也试过了

import { Response } from "@angular/http";

this.http.post(url).post((resp: ResponsE) => resp)

它可以编译,但是得到了不受支持的媒体类型响应。这些解决方案应适用于“ http”,但不适用于“ httpClient”。

编辑2

@Supamiu解决方案也得到了不受支持的媒体类型,因此标题中将出现错误。因此,上面的第二个解决方案(具有Response类型)也应该起作用。但从个性上来说,我认为这不是将“http”与“ httpClient”混合使用的好方法,因此我将保留Supamiu的解决方案

大佬总结

以上是大佬教程为你收集整理的Angular 4.3.3 HttpClient:如何从响应的标头获取值?全部内容,希望文章能够帮你解决Angular 4.3.3 HttpClient:如何从响应的标头获取值?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。