dpv21589 2017-12-06 14:32
浏览 58

Datatable Codeigniter中的JSON响应无效

I need an expert to fix this . This is my controller that load 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);
}

And this is JSON response:

{"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"}

The problem is, when I load it into my data table, show popup invalid JSON response. This is my jQuery code

$(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);
})
  • 写回答

1条回答 默认 最新

  • dongyi6845 2017-12-06 14:37
    关注

    You are printing each result inside the loop. You should print the result as 1 array.

    /* ============= 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); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题