Angular 8 HttpClient:无法处理文本/csv服务器响应

yBo*_*her 1 httpclient http-headers angular

我有一个端点,它返回一个 csv 数据集,该数据集将使用保存saveasBlob\nAngularJS 前端工作正常,而 Angular 8 则很困难。\n这是我的 Angular 8 实现

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

这会导致以下HttpErrorResponse情况:\n Object {error: SyntaxError: Unexpected token 。在 JSON 中,位于 positio\xe2\x80\xa6,文本:“1.112.373;en-US\n日期时间 UTC;日期本地时间;Bat\xe2\x80\xa6”}

\n\n

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

\n\n
Connection: keep-alive\nContent-Length: 141\nPragma: no-cache\nCache-Control: no-cache\nAccept: text/csv\nContent-Type: application/json\nAccept-Encoding: gzip, deflate\nAccept-Language: en-US,en;q=0.9\n
Run Code Online (Sandbox Code Playgroud)\n\n

和响应头:

\n\n
HTTP/1.1 200 OK\nCache-Control: no-cache, no-store, must-revalidate\nPragma: no-cache\nContent-Type: text/csv\nExpires: 0\nServer: Microsoft-IIS/10.0\nX-Powered-By: ASP.NET\nX-UA-Compatible: IE=edge\nContent-Length: 191147\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

yBo*_*her 7

如果有人关心的话:我的怀疑是正确的:响应被认为是类型json。设置responseType='text'是这样进行的:

  const options = { headers, responseType: 'text' as any };
Run Code Online (Sandbox Code Playgroud)

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