我需要专家来解决这个问题。这是我加载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);
})
答案 0 :(得分: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);