返回JSON值时,ajax返回每个字母

时间:2016-03-15 22:31:54

标签: php jquery mysql json ajax

我不确定这可能重复......请帮我解决这个问题

我试图将3个Mysql值赋予jquery ajax函数。但我无法显示值。我已经使用了各种帖子中描述的许多方法。但没有任何东西可供我匹配。

以下是我的php页面

$equipm = array();
$service = array();
$facility = array();
$result = array();
$equip_sel   = "select * from equipment";
$run_equip   = mysqli_query($con,$equip_sel);

while($rowe =mysqli_fetch_assoc($run_equip))
{
    $equipm[] = $rowe;
}
$serv_sel    = "select * from services";
$run_serv    = mysqli_query($con,$serv_sel);
while($rows =mysqli_fetch_assoc($run_serv))
{
    $servc[] = $rows;
}

$faci_sel    = "select * from specility ";
$run_fac     = mysqli_query($con,$faci_sel);
while($rowf =mysqli_fetch_assoc($run_fac))
{
    $facility[] = $rowf;
}

//$result[0]=$equipm;
//$result[1]=$servc;
//$result[2]=$facility;
$result=array('equipment'=>array($equipm), 'services'=> array($servc),     'facility'=> array($facility));
echo json_encode($result);

以下是我的jquery,

   <script type='text/javascript'>
    $(document).ready(function(){
            /* call the php that has the php array which is json_encoded enter code here*/
            $.ajax({
            type:"POST",
            url: "ajax2.php",
            datatype: "json",
            success: function(data) {
            alert(data[0]);
            });

            }
        });

    });
    </script>
上面的

结果是输出表中的每个字符。

请在这里帮忙。每次返回一个字符。

1 个答案:

答案 0 :(得分:0)

由于括号括号和圆括号,jquery中存在语法错误。这是固定版本,对我有用。

<script type='text/javascript'>
    $(document).ready(function(){
        /* call the php that has the php array which is json_encoded enter code here*/
        $.ajax({
            type:"POST",
            url: "ajax2.php",
            datatype: "json",
            success: function(data) {
                alert(data[0]);
            }
        });        
    });

</script>