duanqiang2617
2017-06-03 21:07
浏览 141

SyntaxError:位于0的JSON中的意外标记C - Ionic 2 Http GET请求

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);

图片转代码服务由CSDN问答提供 功能建议

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

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

我收到错误 SyntaxError:位于0的JSON中的意外的令牌C 。 我也假设错误与请求有关。

在我的服务器端,我有这样的数据发送(PHP): < p> echo json_encode($ array);

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douwen3198 2017-06-09 19:19
    已采纳

    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.

    已采纳该答案
    打赏 评论
  • douya7309 2017-06-03 21:11

    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

    打赏 评论

相关推荐 更多相似问题