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);
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' }
{ name: "john" }
OR
{ 'name' = 'john' }
In your case, the JSON begins with character C keep in mind that a valid Javascript object could be invalid JSON form
我只是忽略了我在脚本中仍然有两个echo
语句......这就是为什么它不被识别为 JSON。
in my case:
previous with error: JSON.parse("{ createdTimestamp: -1 }")
and correct: JSON.parse('{"createdTimestamp":-1}')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.