【问题标题】:Parsing PNG from response (without default JSON parsing)从响应中解析 PNG(没有默认的 JSON 解析)
【发布时间】: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


【解决方案1】:

问题其实很简单,正如rickz所指,我不得不补充

{ responseType: 'blob' } 

在 HttpRequest 选项中,不是 HttpHeader(我最初认为),而是 HttpRequest 的单独参数。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    相关资源
    最近更新 更多