Unexpected end of JSON input in Http get request from Angular 2 to Codeigniter
Just ran into this issue with angular 4 when I upgraded to use the new HttpClient from @angular/common/http
The interesting thing with HttpClient is that it automatically runs .json() on your responses. This causes problems when the api returns back an empty response. You'll then get the Unexpected end of json error. The solution is to set responseType as text:
http.get(url, { responseType: 'text' })
You are trying to parse a null value as a JSON
return this._http.get(myUrl)
.map((res: Response ) => res.json())
.catch(this._errorHandler);
If res is null/undefined/empty string you will get the error. Just check for the value of res
before doing res.json()
and you won't get the error. To be sure of this make a console.log(res)
before trying to parse it.