1

im making a http get request to and specific url and the response is a string (base64 images) using this method:

  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);
        });
    });
  }

for some reason i getting the error JSON.parse: unexpected character at line 1 column 1 of the JSON data

Here is the response 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
1
  • could your add the response header to your question, it seems that angular think the response is some JSON and try to parse it. You certainly have a application/json content-type on the response Commented Mar 14, 2018 at 14:28

1 Answer 1

2

The new angular's HTTP module is now in default expecting for JSON response from server. If you want to change it you need to add a proper HTTP header. try to add this header:

{'responseType':'image/*'}

So your get request should look like :

 this.http.get(url,{
 'responseType':'image/*'
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.