我正在使用CodeIgniter和数据表。当记录可用时,我可以在列表中显示数据库中的记录。 但是当没有可用记录时,我会收到JSON错误。
DataTables警告:table id = tableId-无效的JSON响应。有关此错误的更多信息,请参见http://datatables.net/tn/1
我尝试了我的代码
控制器
public function MembershipList(){
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$books=$this->Fees_model->MembershipList();
$draw = 1;
$recordsTotal = count($books);
$recordsFiltered = count($books);
$n=1;
$data = array();
if(!empty($books)){
foreach($books as $row)
{
$action='<button type="button" name="update" id="'.$row->clubMembershipFees_id.'" class="btn btn-warning btn-xs update">Edit</button>';
$data[] = array(
"Sr_No" => $n,
"Duration" => $row->Duration,
"PrimaryMember" => $row->PrimaryMember,
"action" => $action
);
$n++;
}
}
else{
echo "no data available";
}
$output = array(
"draw" => $draw,
"recordsTotal" => $recordsTotal,
"recordsFiltered" =>$recordsFiltered,
"data" => $data
);
echo json_encode($output);
exit;
}
ajax
$('#Membershiplist').DataTable({
"processing":true,
//"serverSide":true,
"ordering": false,
"ajax":{
url:baseUrl+ "/controller_one/MembershipList",
type:"POST",
dataSrc: function (json) {
return json;
}
},
"columns": [
{"data":"Sr_No"},
{ "data": "Duration" },
{ "data": "PrimaryMember" },
{ "data": "action" }
]
//"emptyTable": "No record found",
});
你能帮我解决我的错吗?我在网络标签"no data available{"draw":1,"recordsTotal":1,"recordsFiltered":1,"data":[]}"
由@Shashidhara建议
console.log(json); output
{…}
data: (1) […]
0: {…}
Sr_No: 1
action: "<button type=\"button\" name=\"update\" id=\"1\" class=\"btn btn-warning btn-xs update\">Edit</button>"
Duration: "2019-2020"
PrimaryMember: "100"
<prototype>: Object { … }
length: 1
<prototype>: Array []
draw: 1
recordsFiltered: 1
recordsTotal: 1
答案 0 :(得分:0)
这应该有效。
import React, { PropTypes } from "react";
const Icon = props => {
const styles = {
img: {
width: `${props.size}`,
height: `${props.size}`
}
};
return <img style={styles.img} src={props.icon} />;
};
Icon.propTypes = {
size: PropTypes.string,
icon: PropTypes.string.isRequired
};
Icon.defaultProps = {
size: 32
};
export default Icon;
Javascript:
public function MembershipList(){
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$books=$this->Fees_model->MembershipList();
$draw = 1;
$recordsTotal = count($books);
$recordsFiltered = count($books);
$n=1;
$data = array();
if(!empty($books)){
foreach($books as $row)
{
$action='<button type="button" name="update" id="'.$row->clubMembershipFees_id.'" class="btn btn-warning btn-xs update">Edit</button>';
$data[] = array(
"Sr_No" => $n,
"Duration" => $row->Duration,
"PrimaryMember" => $row->PrimaryMember,
"action" => $action
);
$n++;
}
}
$output = array(
"draw" => $draw,
"recordsTotal" => $recordsTotal,
"recordsFiltered" =>$recordsFiltered,
"data" => $data
);
echo json_encode($output);
}
答案 1 :(得分:0)
您的代码有几个问题:
您已使用“ serverSide
”启用服务器端处理模式:true,但是数据格式化为客户端处理模式。删除"serverSide": true
以使用客户端处理模式。
您需要使用下面显示的dataSrc:“”灰匹配您的JSON数据格式,有关更多信息,请参见dataSrc。
"ajax" : {
"url" : "ListAllUserServlet",
"type" : "POST",
"dataSrc": ""
},