【问题标题】:Angular 8 HttpClient: Unable to process text/csv server responseAngular 8 HttpClient:无法处理文本/csv 服务器响应
【发布时间】:2020-02-21 10:05:51
【问题描述】:

我有一个端点,它返回一个 csv 数据集,它将使用 saveasBlob 保存 AngularJS 前端工作正常,而 Angular 8 却在挣扎。 这是我的 Angular 8 实现

 const headers = new HttpHeaders({
            Accept: 'text/csv',
        });
        const options = { headers };
        this.httpClient
            .post<ExportCsvSettingsViewDto>(this.appConfigService.buildApiUrl(this.loadPath(batchId)), csvDto, options)
            .subscribe((fileResult: any) => {
                const file = new Blob([fileResult], { type: 'text/csv' });
                saveAs(file, this.fileName + '.csv');
            });

这导致HttpErrorResponse 声明: 对象 {error: SyntaxError: Unexpected token 。在 JSON 中的位置...,文本:“1.112.373;en-US 日期时间 UTC;日期本地时间;蝙蝠…"}

我怀疑这是因为HttpClient 将响应视为JSON 而不是"text/csv" 这些是请求标头:

Connection: keep-alive
Content-Length: 141
Pragma: no-cache
Cache-Control: no-cache
Accept: text/csv
Content-Type: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

和响应头:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/csv
Expires: 0
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
X-UA-Compatible: IE=edge
Content-Length: 191147

我尝试将请求标头中的"responseType" 显式设置为"text/csv",但这是不可能的。 有什么想法吗?

【问题讨论】:

    标签: angular http-headers httpclient


    【解决方案1】:

    如果有人关心: 我的怀疑是正确的:响应被认为是json 类型。 设置responseType='text' 的工作方式如下:

      const options = { headers, responseType: 'text' as any };
    

    我认为这实际上是一个大问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      相关资源
      最近更新 更多