【发布时间】:2019-07-18 04:35:46
【问题描述】:
在尝试了这里阅读的许多内容之后,我仍然找不到解决这个特定问题的方法: 我从角度(localhost:4200)向spring(localhost:8080)上的API发出请求,我有一个HttpService可以很好地处理请求,直到我必须发出响应不是JSON而是图像/ .png:
private httpOptions = {
headers: new HttpHeaders({
'Accept': 'image/png',
'Content-Type': 'image/png'
})
};
constructor(private http: HttpService) { }
getVehicleQr(vehicleId: string): Observable<any> {
return this.http.get('/vehicle/qr/' + vehicleId, this.httpOptions);
}
我在 httpOptions 中设置了 Accept 和 Content-Type 标头,但是当我执行 getVehicleQr() 方法时,我得到了这个错误:
这表明 Angular 仍在尝试解析我设置为不解析的 JSON。 有什么建议?完整代码可见here.
我也见过this post.
【问题讨论】:
-
可能不相关,但仍然不正确:不要设置“Content-Type”——它指的是请求内容(没有)。
-
您发布了相关链接。请告诉我们您为什么不使用 { responseType: 'blob' } 并处理返回的 blob。
标签: json angular typescript http httprequest