SyntaxError: Unexpected token C in JSON at position 0 - Ionic 2 Http GET request

青春壹個敷衍的年華 提交于 2020-04-10 17:55:01

问题


I am trying to perform a GET request and retrieve the data from the response.

this.http.get('http://localhost:8888/maneappback/more-items.php').subscribe(res => {
    console.log(res.json());
}, (err) => {
    console.log(err);
});

I am getting the error SyntaxError: Unexpected token C in JSON at position 0. I am also assuming that the error is related to the request.

On my server side, I have the data being sent like this (PHP):

echo json_encode($array);


回答1:


The message you see is that your JSON response is not formatted correctly

GOOD JSON:

{ "name":"John", "age":31, "city":"New York" }

BAD JSON

{ 'name': 'john' }

OR

{ 'name' = 'john' }

In your case, the JSON begins with character C




回答2:


I was just neglecting to realize that I still had two echo statements in the script...that is why it wasn't recognized as JSON.




回答3:


in my case:

previous with error: JSON.parse("{ createdTimestamp: -1 }")

and correct: JSON.parse('{"createdTimestamp":-1}')



来源:https://stackoverflow.com/questions/44348556/syntaxerror-unexpected-token-c-in-json-at-position-0-ionic-2-http-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!