Javascript 语法错误:JSON 中位置 0 的意外标记 C - Ionic 2 Http GET 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44348556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: Unexpected token C in JSON at position 0 - Ionic 2 Http GET request
提问by ewizard
I am trying to perform a GETrequest 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. I am also assuming that the error is related to the request.
我收到错误SyntaxError: Unexpected token C in JSON at position 0。我还假设错误与请求有关。
On my server side, I have the data being sent like this (PHP):
在我的服务器端,我有这样发送的数据(PHP):
echo json_encode($array);
echo json_encode($array);
回答by Gianfrancesco Aurecchia
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' }
OR
或者
{ 'name' = 'john' }
In your case, the JSON begins with character C
在您的情况下,JSON 以字符 C 开头
回答by ewizard
I was just neglecting to realize that I still had two echostatements in the script...that is why it wasn't recognized as JSON.
我只是忽略了我echo在脚本中仍然有两个语句......这就是为什么它不被识别为 JSON。
回答by Nicolas Sturm
in my case:
就我而言:
previous with error: JSON.parse("{ createdTimestamp: -1 }")
以前有错误: JSON.parse("{ createdTimestamp: -1 }")
and correct: JSON.parse('{"createdTimestamp":-1}')
并更正: JSON.parse('{"createdTimestamp":-1}')

