0

我无法使用 ajax 调用从我的 php 脚本中检索 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 脚本没有任何错误。

4

1 回答 1

0

为 json 数据输出添加适当的标头也很好(例如在脚本的开头)。

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

至于查询结果,你应该调试它。尝试打印查询并在 Phpmyadmin(或其他数据库管理工具)中运行它

于 2012-12-29T20:08:21.980 回答