weixin_33686714 2015-04-12 14:11 采纳率: 0%
浏览 62

为什么返回的数据没有得到正确的警告?

我在向服务器上的php脚本发送Ajax POST请求,然后服务器以JSON格式返回数据。但当我试图alert结果时,我会得到以下错误:Uncaught SyntaxError: unexpected token {——两次。

这里是我的Ajax调用:

var articles = $.post("process/get_articles.php");
    articles.done(function(data){
        var result = $.parseJSON(data);
        alert(result);
    });

我的服务器端代码:

while($query->fetch()){
    $result = array("ID"=>$Art_number, "Article"=>$Article, "Image"=>$Image_link);
    $result = json_encode($result);
    echo $result;
}

返回以下内容:

{"ID":1,"Article":"Article 1","Image":"http:\/\/wwww.mydomain.com\/images\/img.jpg"}{"ID":2,"Article":"Article2","Image":""}{"ID":3,"Article":"Article 3","Image":""}

为什么返回的数据没有得到正确的警告?任何帮助都将不胜感激!

  • 写回答

4条回答 默认 最新

  • 普通网友 2015-04-12 14:14
    关注

    Your code is spitting out successive JSON objects back-to-back. The result, overall, is not valid JSON.

    Put your arrays in an enclosing single array and then JSON-encode that as the response. That'll result in the client getting an array of objects, which will be valid.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿