Angular HttpClient ResponseTyp错误

时间:2018-03-05 20:25:22

标签: angular http ionic-framework httpclient

我尝试从api下载图像(png)。我的问题是,由于某种原因,只有json文件被ionic / angular接受。

return this.http.get(this.authKeys.serverURL, {headers: this.header, responseType: ResponseContentType.Blob});

发生以下错误:

 Argument of type '{ headers: any; responseType: ResponseContentType.Blob; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.   Types of property 'responseType' are incompatible.     Type 'ResponseContentType.Blob' is not assignable to type '"json"'.

出于某种原因,只允许json类型的响应......

2 个答案:

答案 0 :(得分:9)

responseType包含字符串值。所以你应该传递这些值中的任何一个

'arraybuffer' | 'blob' | 'json' | 'text';

在早期版本的Angular 2中,它的API设计为期望responseType的枚举值,check here

答案 1 :(得分:4)

对我来说,它使用“ as'json'”语法:

responseType: 'blob' as 'json'