在哪里将 HttpClient 的 responseType 设置为 http GET 角度

Dre*_*w13 1 angular

我有一个向我的后端发出 http 调用的服务:

exportSubs(param: Param): Observable<Sub[]> {
    return this.http.get<Sub[]>(
      `${environment.apiBaseUrl}/blah`,
      {headers: this.httpUtil.getReqHeaders})
      .catch(error => this.httpUtil.handleError(error));
  }
Run Code Online (Sandbox Code Playgroud)

我在哪里设置 responseType?

Saj*_*ran 6

您可以使用responseType. 请参阅请求非 JSON 数据

在您的示例中,您应该能够使用:

return this.http.get(
    `${environment.apiBaseUrl}/blah`, { responseType: 'text' })
Run Code Online (Sandbox Code Playgroud)

编辑

您可以将 responseType 设置为 blob,

return this.http.get(`${environment.apiBaseUrl}/blah`, { responseType: 'blob' });
Run Code Online (Sandbox Code Playgroud)

  • 这在语法上甚至无效,所以我怀疑您是否尝试过。 (4认同)