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
..
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:
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.
Hi @riteshbhat17,
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.
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.
echo json_encode(array('up_votes'=>$up_votes, 'down_votes'=> $down_votes ));
this is the php response
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.
sorry boss…not working.
Receiving the same error.
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
Oh ok…now i get it.
The response is in HTML …god knows how…
Can you tell something to resolve this?
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
such a silly mistake…
Thanks @nb1990