Angular SyntaxError JSON.parse: 意外的字符

Angular SyntaxError JSON.parse: unexpected character

我正在向特定 url 发出 http get 请求,响应是一个字符串(base64 图像),使用此方法:

  public getSingleImage(imgId): Promise<any> {
    return new Promise((resolve, reject) => {
      const url = `${CONST.DOCUMENT_SINGLE_IMAGE}/${imgId}` + '/base64';
      this.http.get(url)
        .toPromise()
        .then((res: any) => {
          // value = res;
          resolve(res);
        }).catch((error) => {
          console.log(error);
        });
    });
  }

出于某种原因我收到错误 JSON.parse: unexpected character at line 1 column 1 of the JSON data

这是回复header:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:4200
api-supported-versions: 1.0
Content-Type: text/plain; charset=utf-8
Date: Wed, 14 Mar 2018 14:01:07 GMT
Server: Kestrel
Transfer-Encoding: chunked
Vary: Origin

new angular's HTTP module 现在默认等待来自服务器的 JSON 响应。如果你想改变它,你需要添加一个正确的 HTTP header。 尝试添加此 header:

{'responseType':'image/*'}

因此您的获取请求应该如下所示:

 this.http.get(url,{
 'responseType':'image/*'
})