# Error in receiving JSON object from API

**URL:** <https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021>\
**Category:** ionic-v3\
**Created:** [June 5, 2017, 7:39am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021 "2017-06-05T07:39:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![riteshbhat17](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@riteshbhat17](https://forum.ionicframework.com/u/riteshbhat17)\
**Post date:** [June 5, 2017, 7:39am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/1 "2017-06-05T07:39:08Z")

</div>

This is the object which i am receiving from API  
`{"up_votes":"1","down_votes":"0"}`

using http.get to receive data in app, home.ts

```auto
..
public vote_response:any={};
...
this.http.get(this.baseURI+'votesNum.php?'+this.question.question_id)
        .subscribe(result =>{this.vote_response = result.json();});

```

this is the error which i am receiving:

```auto
ERROR SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at Response.Body.json (http.es5.js:800)
    at MapSubscriber.project (forum-quest-details.ts:44)
    at MapSubscriber._next (map.js:77)
    at MapSubscriber.Subscriber.next (Subscriber.js:89)
    at XMLHttpRequest.onLoad (http.es5.js:1229)
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (core.es5.js:4119)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)

```

Can anyone help me solve this issue.

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/addwebsolution/32/120392_2.png) [@addwebsolution](https://forum.ionicframework.com/u/addwebsolution)\
**Post date:** [June 5, 2017, 8:07am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/2 "2017-06-05T08:07:32Z")

</div>

Hi @riteshbhat17,

```auto
this.http.get(this.baseURI+'votesNum.php?'+this.question.question_id)
        .map(res=>res.json())
        .subscribe(result =>{this.vote_response = result});

```

Try this code. There is little alteration in your code.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 5, 2017, 8:36am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/3 "2017-06-05T08:36:23Z")

</div>

I don’t see much value in the previous comment. OP is blindly calling `json()` on the response, and for whatever reason, the backend is not returning JSON, so it is failing. The fact that the unexpected token is ‘\<’ leads me to believe that the backend is returning HTML.

---

<div class="post-metadata">

**Author:** ![riteshbhat17](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@riteshbhat17](https://forum.ionicframework.com/u/riteshbhat17)\
**Post date:** [June 5, 2017, 8:38am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/4 "2017-06-05T08:38:11Z")

</div>

`echo json_encode(array('up_votes'=>$up_votes, 'down_votes'=> $down_votes ));`  
this is the php response

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 5, 2017, 8:40am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/5 "2017-06-05T08:40:47Z")

</div>

Look at what is going over the wire via Chrome’s developer tools. It cannot be what you think it is, or else you would not be seeing this error.

---

<div class="post-metadata">

**Author:** ![riteshbhat17](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@riteshbhat17](https://forum.ionicframework.com/u/riteshbhat17)\
**Post date:** [June 5, 2017, 8:43am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/6 "2017-06-05T08:43:57Z")

</div>

sorry boss…not working.  
Receiving the same error.

---

<div class="post-metadata">

**Author:** ![riteshbhat17](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@riteshbhat17](https://forum.ionicframework.com/u/riteshbhat17)\
**Post date:** [June 5, 2017, 8:47am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/7 "2017-06-05T08:47:44Z")

</div>

this is the output for console.log(res)  
the **\_body** :  
`"<br />↵<b>Notice</b>: Undefined index: question_id in <b>C:\xampp\htdocs\success\team\votesNum.php</b> on line <b>5</b><br />↵{"up_votes":"0","down_votes":"0"}"`  
hope you can tell something now

---

<div class="post-metadata">

**Author:** ![riteshbhat17](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@riteshbhat17](https://forum.ionicframework.com/u/riteshbhat17)\
**Post date:** [June 5, 2017, 8:50am UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/8 "2017-06-05T08:50:12Z")

</div>

Oh ok…now i get it.  
The response is in HTML …god knows how…  
Can you tell something to resolve this?

---

<div class="post-metadata">

**Author:** ![nb1990](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nb1990](https://forum.ionicframework.com/u/nb1990)\
**Post date:** [June 5, 2017, 3:20pm UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/9 "2017-06-05T15:20:51Z")

</div>

in your php are you doing something like this `$question_id = $GET['question_id']`? if you are you need to change this `votesNum.php?'+this.question.question_id` to `votesNum.php?question_id= + this.question.question_id`

---

<div class="post-metadata">

**Author:** ![riteshbhat17](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@riteshbhat17](https://forum.ionicframework.com/u/riteshbhat17)\
**Post date:** [June 5, 2017, 4:19pm UTC](https://forum.ionicframework.com/t/error-in-receiving-json-object-from-api/93021/10 "2017-06-05T16:19:40Z")

</div>

such a silly mistake… 😑  
Thanks @nb1990
