0

I am making an http request from ionic app(Angular4) and receiving a response in form of a Js object, but on receiving i am getting some error.

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.

6
  • You are probably not receiving the object you think. Debug the code and check. Commented Jun 5, 2017 at 8:11
  • Arg0n has a point. It is possible you are receiving and xml. Something hints on the first character being "<" Commented Jun 5, 2017 at 8:12
  • Try JSON.stringify the result and console.log it, and then see what it says. It could be several things like your parameters are wrong and its sending an html page in return or something Commented Jun 5, 2017 at 8:13
  • I think with that request you are fetching a php file instead of a JSON string, that's why you get < at position 0. You should check if the url this.baseURI+'votesNum.php?'+this.question.question_id is correct. Commented Jun 5, 2017 at 8:26
  • Yes you are correct Arg0n. I am getting 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"} I am getting the response from php server by using echo json_encode(array('up_votes'=>$up_votes, 'down_votes'=> $down_votes )); can anyone tell how to solve this ? Commented Jun 5, 2017 at 8:56

1 Answer 1

1
this.vote_response = result.json();

Replace this one with console.log(result) and take the data and check with some online JSON validation site (https://jsonlint.com) to see if the data is valid JSON. You might be getting HTML body instead of JSON.

To response as JSON:

header('Content-Type: application/json');
echo  json_encode(array('up_votes'=>$up_votes, 'down_votes'=> $down_votes ));
Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are correct. I am getting 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"} I am getting the response from php server by using echo json_encode(array('up_votes'=>$up_votes, 'down_votes'=> $down_votes )); can anyone tell how to solve this ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.