【问题标题】:Angular HttpClient ResponseTyp ErrorAngular HttpClient ResponseTyp 错误
【发布时间】:2018-03-05 20:25:22
【问题描述】:

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

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 类型的响应...

【问题讨论】:

标签: angular http ionic-framework httpclient


【解决方案1】:

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

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

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

【讨论】:

  • 即使我使用这四个字符串之一,我也会遇到同样的错误。有什么建议吗?
【解决方案2】:

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

responseType: 'blob' as 'json'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 2018-05-26
    相关资源
    最近更新 更多