2016-03-15 142 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>