【问题标题】:ajax returns each letter when return JSON value issueajax 在返回 JSON 值问题时返回每个字母
【发布时间】:2016-07-01 14:45:09
【问题描述】:

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

我正在尝试为 jquery ajax 函数获取 3 个 Mysql 值。但我无法显示这些值。我已经使用了各种帖子中描述的许多方法。但没有任何东西适合我。

下面是我的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>

以上结果来自输出表中的每个字符。

请在此处提供帮助。每次返回一个字符。

【问题讨论】:

  • 它的作用:显示所有内容或只显示一个字符(问题提出不当)...
  • 我现在无法检查代码,我认为这里有问题。你的 '$equipm' 已经是一个数组,你的结果看起来像 'array('equipment'=>array(array(...));'。这符合你的期望吗?你的 jQuery 警报 'data[0]' ,但我建议 json 会有'data.equipment'。

标签: php jquery mysql json ajax


【解决方案1】:

由于右括号和圆括号,您的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-01
    • 2020-12-12
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    相关资源
    最近更新 更多