Angular HttpClient ResponseTyp错误

Kli*_*itz 5 http httpclient ionic-framework angular

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

return this.http.get(this.authKeys.serverURL, {headers: this.header, responseType: ResponseContentType.Blob});
Run Code Online (Sandbox Code Playgroud)

发生以下错误:

 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"'.
Run Code Online (Sandbox Code Playgroud)

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

Pan*_*kar 10

responseType持有字符串值.所以你应该传递这些值中的任何一个

'arraybuffer' | 'blob' | 'json' | 'text';
Run Code Online (Sandbox Code Playgroud)

在早期版本的Angular 2中,它的API设计为期望枚举值responseType,请在此处查看

  • 即使我使用这四个字符串之一,我也会遇到同样的错误。有什么建议? (2认同)

Tom*_*nik 7

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

responseType: 'blob' as 'json'
Run Code Online (Sandbox Code Playgroud)