Php 无法在jquery中访问或打印Ajax响应

Php 无法在jquery中访问或打印Ajax响应,php,jquery,json,ajax,Php,Jquery,Json,Ajax,我从我的ajax调用中得到了一个响应,但是我如何访问或至少打印响应呢 这是我的ajax电话 $.ajax({ type: "POST", url: '../backorderReport.php', data: { from : from, to : to }, dataType: "JSON", success: function(data){ console.l

我从我的ajax调用中得到了一个响应,但是我如何访问或至少打印响应呢

这是我的ajax电话

$.ajax({
         type: "POST",
         url: '../backorderReport.php',
         data: { from : from, to : to  },
         dataType: "JSON",
         success: function(data){
                    console.log(data[0].orderID); //not printing anything
                    console.log(data);            //not printing anything
         }
 });
这是我的php文件

$from = $_POST["from"];
$to = $_POST["to"];
$orders = $wpdb->get_results("SELECT * FROM wp_orderrecords WHERE orderDate BETWEEN '".$from."' AND '".$to."'");

foreach($orders as $order){
            $orderID = $order->orderID;
            $orderDate = $order->orderDate;
            $status = $order->status;
            $clientID = $order->clientID;
            $bill = $order->bill;
            $ship = $order->ship;
            $pay = $order->pay;
            $total = $order->total;
        $results = $wpdb->get_results("SELECT * FROM wp_clients WHERE clientID = '".$clientID."%'");     

           foreach($results as $order){
                  $clientsName = $order->clientsName;
           } 

           $c = str_replace(' ', " ", $clientsName);

           $orderItem = array(
                    'orderID' => $orderID,
                    'orderDate' => $orderDate,
                    'orderStatus' => $status,
                    'clientsName' => $c,
                    'bill' => $bill,
                    'ship' => $ship,
                    'pay' => $pay,
                    'total' => $total
                 );

           echo json_encode($orderItem);
}
我得到了这样的回应

{"orderID":"26","orderDate":"2016-05-11","orderStatus":"Active","clientsName":"Pebbe\u00a0Kristel\u00a0A
\u00a0Bunoan","bill":"Billed","ship":"Delivered","pay":"Unpaid","total":"1200.00"}{"orderID":"27","orderDate"
:"2016-05-13","orderStatus":"Completed","clientsName":"Lovely\u00a0Carbon","bill":"Billed","ship":"Delivered"
,"pay":"Paid","total":"4650.00"}
如何打印响应并将数据放入表中?谢谢你的帮助

使用JSON.parse

success: function(data){ 
        var parsedData = JSON.parse(data);
     }

您需要解析json字符串

success: function(data){
    var resultArray = $.parseJSON(json);
    console.log(resultArray[0].orderID); 
    console.log(resultArray);            
}

对于与查询匹配的每个记录,您将回显一个有效的json字符串。因此得到如下结果:

{ record_1 } { record_2 }
这是无效的json。您需要一个如下所示的数组:

[{ record_1 },{ record_2 }]
对php稍作修改,如下所示:

 $from = $_POST["from"];
 $to = $_POST["to"];
 $orders = $wpdb->get_results("SELECT * FROM wp_orderrecords WHERE orderDate BETWEEN '".$from."' AND '".$to."'");

$records = [];      // adds an array for the records

foreach($orders as $order){
        $orderID = $order->orderID;
        $orderDate = $order->orderDate;
        $status = $order->status;
        $clientID = $order->clientID;
        $bill = $order->bill;
        $ship = $order->ship;
        $pay = $order->pay;
        $total = $order->total;
    $results = $wpdb->get_results("SELECT * FROM wp_clients WHERE clientID = '".$clientID."%'");     

       foreach($results as $order){
              $clientsName = $order->clientsName;
       } 

       $c = str_replace(' ', " ", $clientsName);

       $orderItem = array(
                'orderID' => $orderID,
                'orderDate' => $orderDate,
                'orderStatus' => $status,
                'clientsName' => $c,
                'bill' => $bill,
                'ship' => $ship,
                'pay' => $pay,
                'total' => $total
             );
       $records[] = $orderItem;  // push each order item on the array
 }
 echo json_encode($records);    // echo the array

ps:在回显之前测试边界条件,例如,未找到与查询匹配的记录,以及服务器端软件中可能存在的其他异常情况。

使用var resultArray=$.parseJSON(数据);如果
console.log(data)
没有返回任何信息,那么您可能没有收到服务器的响应。@DainisaTools,为什么我调用ajax时,控制台会显示响应json以及标题、post、html和cookies。我该怎么办?我编辑了上面的问题以显示我是如何进行json_编码的。@YvesLeBorg,我编辑了我的问题以显示我是如何进行json_编码的。我做得对吗?不是循环,他需要访问
orderID
parsedData.orderID
parsedData['orderID']
嗨!我在控制台中仍然没有得到任何东西。为
success
函数指定
data
对象参数将自动从字符串解析为对象形式。@user3383911如果这个答案对您有帮助,请接受它。我在控制台中仍然没有得到任何东西。很高兴这有帮助。不要让php bug咬你:)