2012-12-29 79 views
0

我無法使用ajax調用從我的php腳本檢索json字符串數據。無法從php中使用ajax檢索json字符串

這裏是我的AJAX腳本:

$.ajax({ 
      type: "POST", 
      async: false, 
      dataType: "json", 
      url: "database/clientpanel/logs/search_call_log.php", 
      data: { 
       from: from, 
       to: to, 
       sel: sel 
       }, 
      cache: false, 
      success: function(data){ 
       $("#app_panel").append(data.html); 
       $('.inv_date').hide(); 
      } 
     }); 

,這是我的PHP腳本:

<?php 
//wall =================================================== 
session_start(); 
include("../../dbinfo.inc.php"); 
    $from = $_POST['from']; 
    $to = $_POST['to']; 
    $sel = $_POST['sel']; 
// connect to the database 
    $client_id = $_SESSION['clientid']; 
    $out = 0; 
    $in = 0; 
    $ext =0; 
    $min = 0; 
    $sec = 0; 
    $results = array(
    'html' => $html 
);  

    $html = " "; 
    if($sel == "all"){ 
    $query=" select * from call where client='$client_id' ORDER BY date_time DESC"; 
    $result = $mysqli->query($query); 
    }else{ 
    $query=" select * from tele_panel_call where (client='$client_id' AND date_time BETWEEN '$from' AND '$to') ORDER BY date_time DESC"; 
    $result = $mysqli->query($query); 
    } 
     if ($result->num_rows > 0){ 

             while ($row = $result->fetch_object()) 
             { 
             $from = $row->from; 
             $to = $row->to; 

            $html .= '<div style="width:590px;height:15px;background: url(img/clientimg/wrap-white.png)repeat;padding: 5px 5px 5px 5px;margin-bottom:5px;">'; 

              $query_from=" select * from tele_agent_dialer where (client='$client_id' AND (dialer='$from' OR dialer='$to'))"; 
              $result_from = $mysqli->query($query_from); 
              $row_from = $result_from->fetch_assoc(); 
              $dialer = $row_from['dialer']; 
              if($dialer == $from){ 
              $image = 'outgoing'; 
              $out = $out+1; 
              } 
              if($dialer == $to){ 
              $image = 'incoming'; 
              $in = $in+1; 
              } 
              if($dialer != $to & $dialer != $from){ 
              $image = 'extension'; 
              $ext = $ext+1; 
              } 
            $html .= '<img src="img/clientimg/'; $html .= $image; $html .= '.png" style="float:left;margin-right:10px;height:15px">'; 
            $html .= '<div style="float:left;margin-right:5px;width:135px;height:30px;overflow:hidden;"><b>From: </b>'; 
              if( preg_match('/^\d(\d{3})(\d{3})(\d{4})$/', $from, $matches)) 
              { 
               $from = '('. $matches[1] . ') ' .$matches[2] . '-' . $matches[3]; 
              } 
              $html .= $from; 
            $html .= '</div> 
              <div style="float:left;margin-right:5px;width:125px;height:30px;overflow:hidden;"> 
              <b>To: </b>'; 
              if( preg_match('/^\d(\d{3})(\d{3})(\d{4})$/', $to, $matches)) 
              { 
               $to = '('. $matches[1] . ') ' .$matches[2] . '-' . $matches[3]; 
              } 
              $html .= $to; 
            $html .= '</div> 
              <div style="float:left;width:160px;margin-right:5px;height:30px;overflow:hidden;"> 
              <b>Date/Time: </b>'; $html .= $row->date_time; 
            $html .= '</div> 
              <div style="float:left;width:100px;margin-right:5px;height:30px;overflow:hidden;"> 
              <b>Duration: </b>'; 
              $duration = $row->duration; 
              preg_match("#(\d+):(\d+)#", $duration, $matches); 
              $min = $min + $matches[1]; 
              $sec = $sec + $matches[2]; 
              $html .= $duration; 
            $html .= '</div>'; 
            $html .= '</div>'; 

             } 

    }else{ 
    echo "No results to display!"; 
    } 



$jsonString = json_encode($results); 
echo $jsonString; 

$mysqli->close(); 
?> 

有人可以告訴我,我做錯了什麼嗎?當我檢查頁面本身時,我的php腳本沒有任何錯誤。

+0

是PHP腳本和這個阿賈克斯在同一個域? –

+0

很顯然,這是造成這種情況的PHP腳本,或者它不會顯示「No results to display!」。你的數據庫中是否有實際的數據? –

+0

@DavidHarris它說在實際頁面上沒有結果,這意味着腳本沒有錯誤。 – telexper

回答

0

另外,爲json數據輸出添加適當的頭文件是很好的(例如在腳本開始時)。

header("Content-Type: application/json"); 

至於查詢結果,你應該調試它。嘗試打印查詢並運行它在Phpmyadmin(或其他數據庫管理工具)