dpz90118 2013-07-25 12:51
浏览 71

通过jQuery Ajax读取JSON数据(由PHP创建)导致“parserror”

I create JSON-data with the following php code:

if($res =  $db->query('Select row1 from table1')){
    while($row = $res->fetch_row()){
        $json[] = $row;
    }
}
sort ($json);
$json = json_encode($json);
echo $json;

The result is [["1"],["2"],["3"]].
When I try to fetch this data with jquery ajax

<div id="output">JSON will be put here</div>
<script language="javascript" type="text/javascript">
    $(function ()   {
    $.ajax({
                url: 'json.php',
                dataType: 'json',
                data: '', 
                error: function(request,error) {
                            alert(error);
                            },
                success: function(data) {
                    var json = data[0];
                    alert(json);
                    $('#output').html(json+", ");
                    }
                });
            });

it says: "parseerror".
I searched a lot (here at Stack Overflow), but my jQuery version seem to be right (1.7.2) and reformating the JSON-outpu did not help (I deleted the opening brackets and tried a lot of other things).
Any ideas?

  • 写回答

4条回答 默认 最新

  • dsi37923 2013-07-25 12:55
    关注

    Parse the data return in ajax result,

    var retData= JSON.parse(data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog