2012-12-29 46 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(或其他数据库管理工具)