【问题标题】:Invalid JSON Response in Datatable Codeigniter数据表 Codeigniter 中的无效 JSON 响应
【发布时间】:2017-12-06 14:32:59
【问题描述】:

我需要专家来解决这个问题。这是我加载 JSON 的控制器。

foreach ($result->result() as $row){
    $customer = $row->customer_name;
    $ipull = $row->ip;
    if ($this->mikrotikext->mikrotik_connect($ip,$username,$password,$port) == true){
        $PING = $this->routerosapi->comm("/ping", array(
            "address" => $ipull,
            "count" => "2"
        ));
        if( $PING['0']['packet-loss'] == 0){
            $status = "Online";
        } else {
            $status = "Offline";
        }
    } else {
        $this->session->set_flashdata('Connect_False','Failed To get');
        redirect('tmikrotik/router_list');
    }
    $data = array(
        'customer' => $customer,
        'address' => $ipull,
        'status' => $status
    );
    print json_encode($data);
}

这是 JSON 响应:

{"customer":"Trakindo Utama","address":"192.168.1.3","status":"Online"}{"customer":"Adira Finance","address":"192.168.1.10","status":"Offline"}{"customer":"Mandala Finance","address":"192.168.1.50","status":"Online"}

问题是,当我将它加载到我的数据表中时,显示弹出无效 JSON 响应。这是我的 jQuery 代码

$(function () {
    var table = $("#cpe-status").DataTable({
        fixedColumns: true,
        fixedHeader: true,
        "pageLength": 10,
        "paging": true,
        "ajax": {
            url: "./cpe",
            type: "GET",
            dataSrc: ""
        },
        "scrollX": true,
        "aoColumns": [
            {"data": "customer", "title": "Host"},
            {"data": "address", "title": "Customer Name"},
            {"data": "status", "title": "Registered"}
            // {"data": "status", "title": "Status"}
        ]
    });
    // setInterval(function () {
    //     table.ajax.reload();
    // }, 10000);
})

【问题讨论】:

  • 这绝对是无效的 JSON。返回的 JSON 对象之间应该有逗号,整个东西应该是一个数组。您应该检查 json_encode 的具体用途。
  • 感谢您的回复,那我该怎么办?我需要解决方案而不是解释。我只是一个新手:)
  • @EdoVV 请在下面查看我的答案。

标签: php jquery json datatable


【解决方案1】:

您正在循环内打印每个结果。您应该将结果打印为 1 个数组。

/* ============= Declare the data outside the loop ============= */
$data = array(); 

foreach ($result->result() as $row){
    $customer = $row->customer_name;
    $ipull = $row->ip;
    if ($this->mikrotikext->mikrotik_connect($ip,$username,$password,$port) == true) {
      $PING = $this->routerosapi->comm("/ping", array(
        "address" => $ipull,
        "count" => "2"
      ));
      if( $PING['0']['packet-loss'] == 0){
        $status = "Online";
      } else {
         $status = "Offline";
      }
    } else {
      $this->session->set_flashdata('Connect_False','Failed To get');
      redirect('tmikrotik/router_list');
    }

    /* ============= Push each result on an array ============= */
    $data[] = array(
      'customer' => $customer,
      'address' => $ipull,
      'status' => $status
    );

  }


/* =============  print result (outside the loop ) ============= */
print json_encode($data); 

【讨论】:

  • [{"customer":"Trakindo Utama","address":"192.168.1.3","status":"在线"}][{"customer":"Trakindo Utama","地址":"192.168.1.3","状态":"在线"},{"客户":"Adira Finance","地址":"192.168.1.10","状态":"离线"}][{"客户":"Trakindo Utama","address":"192.168.1.3","status":"在线"},{"customer":"Adira Finance","address":"192.168.1.10","status" :"Offline"},{"customer":"Mandala Finance","address":"192.168.1.50","status":"Online"}] ...响应显示这样,仍然无效json
  • 你是否在循环外声明了$data
  • 您还应该在循环外打印$data
  • 是的,我在循环之前声明了$data,但结果相同
  • 是的。控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 2016-09-06
  • 2017-09-23
  • 2021-11-09
  • 2021-11-28
  • 1970-01-01
  • 2019-07-31
相关资源
最近更新 更多