【问题标题】:Angular 10 append X-Requested-With: XMLHttpRequest to the headerAngular 10 将 X-Requested-With: XMLHttpRequest 附加到标头
【发布时间】:2021-12-29 05:54:18
【问题描述】:

我编写了一个调用旧 API 的 Angular 服务。我需要将标题格式化为如下所示,其中“X-Requested-With”显示在底部(这是一个要求,不是选择)。这是原始请求的图片:

这是我的代码的 sn-p,它确实运行并返回 200 状态:

  private _headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; 
   charset=UTF-8');
  myRequest(data: string): Observable<string> {
    return this.http.post<string>(this.url, data, { headers: this._headers });
  }

将 X-Requested-With: XMLHttpRequest 附加到标头的正确方法是什么。谢谢您的帮助。这是我到目前为止得到的图像:

【问题讨论】:

    标签: angular header angular-services


    【解决方案1】:

    HttpHeaders in Angular are immutable

    这意味着您可以在 myRequest 函数中设置 X-Requested-With 标头。

    myRequest(data: string): Observable<string> {
      const headers = this._headers.set('X-Requested-With', 'XMLHttpRequest');
      return this.http.post<string>(this.url, data, { headers });
    }
    

    它不会改变你的private _headers

    你可以inspect the headers in this example

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2012-10-17
      • 2011-12-31
      相关资源
      最近更新 更多