SyntaxError:JSON位置0处出现意外的令牌C-Ionic 2 Http GET请求

ewi*_*ard 2 javascript php json ionic-framework angular

我正在尝试执行GET请求并从响应中检索数据。

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

我得到了错误SyntaxError: Unexpected token C in JSON at position 0。我还假设该错误与请求有关。

在服务器端,我像这样(PHP)发送数据:

echo json_encode($array);

ewi*_*ard 7

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


Gia*_*hia 6

您看到的消息是JSON响应的格式不正确

良好的JSON:

{ "name":"John", "age":31, "city":"New York" }
Run Code Online (Sandbox Code Playgroud)

错误的JSON

{ 'name': 'john' }
Run Code Online (Sandbox Code Playgroud)

要么

{ 'name' = 'john' }
Run Code Online (Sandbox Code Playgroud)

在您的情况下,JSON以字符C开头