【问题标题】:Angular HttpClient - Failed to pass params for the response of Blob typeAngular HttpClient - 无法为 Blob 类型的响应传递参数
【发布时间】:2023-01-18 09:00:24
【问题描述】:

我的后台API是生成Excel。但是当我在 Angular 中设置参数传递时,在控制台中传递参数不起作用。如何为 blob 类型的请求传递参数?

ExportMyTicket():Observable<Blob> {
  let params = new HttpParams();

  //Parameter passing failure;
  params.append('myParam', '111');

  return this.http.get<Blob>(`${this.exportExcelUrl}`,
    { params, responseType: 'blob' as 'json' })
}

我试过在前端传参数,怎么传都传不过去,怎么设置参数。

【问题讨论】:

    标签: angular typescript http angular-httpclient


    【解决方案1】:

    来自HttpParams description

    这个类是不可变的;所有变异操作都会返回一个新实例。

    1. 您应该使用从.append() 返回的结果重新分配给params。

    2. 从 responseType: 'blob' 中删除 as: 'json'。

      ExportMyTicket():Observable<Blob> {
        let params = new HttpParams();
      
        //Parameter passing failure;
        params = params.append('myParam', '111');
      
        return this.http.get<Blob>(`${this.exportExcelUrl}`,
          { params: params, responseType: 'blob' })
      }
      

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2021-07-25
    • 2021-11-27
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2021-02-12
    相关资源
    最近更新 更多