2016-03-15 249 views
-2

我不知道这可能是重复的每个字母......请帮我在这个问题上AJAX返回时返回JSON值问题

想获得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> 

上述结果从输出表中的每一个字符。

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

+0

它能做什么:显示所有或只显示一个字符(提出问题不好)... –

+0

我现在不能检查代码,我觉得这里有些不妥。你的'$ equipm'已经是一个数组,你的结果看起来像'array('equipment'=> array(array(...));'。 ,但我建议json将有'data.equipment'。 – Wizard

回答

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>