【问题标题】:HTTP Request Headers does not contain custom headers in angularHTTP 请求标头不包含 Angular 中的自定义标头
【发布时间】:2020-02-07 10:31:36
【问题描述】:

这是我的角度服务方法。

public checkIn(checkInRequest: CheckInRequest): Observable<BookingRoomDetails> {
    let _headers = new HttpHeaders();
    _headers = _headers.append('Content-Type', 'application/json'); 
    let options = {      
      headers: _headers,
      withCredentials: true
    }
    const url = this._constantService.checkIn;
    return this._httpClient.post<BookingRoomDetails>(url, checkInRequest, options);
  }

当我控制台记录标题时,我得到了这个。

但是当我从 Chrome 的网络选项卡中打开请求标头时,我找不到自定义标头。

有没有办法添加自定义标题?我知道 HttpHeaders 是不可变的……但它不起作用……

【问题讨论】:

    标签: angular typescript header httpclient


    【解决方案1】:

    @angular/common/http 导入HttpHeaders。然后,

    let header = new HttpHeaders();
    header.set('Content-Type', this.contentType);
    return this._httpClient.post<BookingRoomDetails>(url, checkInRequest,{headers: header, withCredentials: true});
    

    或者简单地说,

    return this._httpClient.post<BookingRoomDetails>(url, checkInRequest,{headers: {'Content-Type', this.contentType}, withCredentials: true});
    

    【讨论】:

      猜你喜欢
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2014-08-08
      • 2011-03-04
      • 1970-01-01
      • 2018-02-09
      相关资源
      最近更新 更多