简体   繁体   English

语法错误:JSON 中位置 0 的意外标记 C - Ionic 2 Http GET 请求

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

I am trying to perform a GET request and retrieve the data from the response.我正在尝试执行GET请求并从响应中检索数据。

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 .我收到错误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):在我的服务器端,我有这样发送的数据(PHP):

echo json_encode($array);

The message you see is that your JSON response is not formatted correctly您看到的消息是您的 JSON 响应格式不正确

GOOD JSON:好的 JSON:

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

BAD JSON错误的 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在您的情况下,JSON 以字符 C 开头请记住,有效的 Javascript 对象可能是无效的 JSON 形式

我只是忽略了我在脚本中仍然有两个echo语句......这就是为什么它不被识别为 JSON。

in my case:就我而言:

previous with error: JSON.parse("{ createdTimestamp: -1 }")前一个错误: JSON.parse("{ createdTimestamp: -1 }")

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM