jquery数据表中的无效json响应
Posted
技术标签:
【中文标题】jquery数据表中的无效json响应【英文标题】:Invalid json response in jquery datatable 【发布时间】:2017-04-04 21:02:59 【问题描述】:我第一次尝试使用 jquery 数据表。在参考手册等后,我能够编写下面的程序,但我不断收到错误消息,指出 json 响应无效。
模型文件:
public function inbox($data)
$con = mysqli_connect("localhost", "root", "","mailman");
$sentFromEmail = $data['sentFromEmail'];
$querySender = "SELECT userId FROM users WHERE userEmail= '$sentFromEmail'";
$resSender = mysqli_query($con, $querySender);
$rowSender = mysqli_fetch_assoc($resSender);
$columnSender = $rowSender["userId"];
$querySender = "SELECT mailId,mailSender,mailSubject,mailContent, mailSendDate FROM mails WHERE mailReceiver = '$columnSender'";
$resSender = mysqli_query($con, $querySender);
$rowSender = mysqli_fetch_assoc($resSender);
$myMail = array();
$test = array();
while($row = mysqli_fetch_array($resSender))
$senderId = $row['mailSender'];
$querySenderName = "SELECT userName FROM users WHERE userId= '$senderId'";
$resSenderName = mysqli_query($con, $querySenderName);
$rowSenderName = mysqli_fetch_assoc($resSenderName);
$columnSenderName = $rowSenderName["userName"];
$test[] = $row;
$myMail[] = array(
'mailId' => $row['mailId'],
'mailSender' => $columnSenderName,
'mailSubject' => $row['mailContent'],
'mailContent' => $row['mailSubject'],
'mailSendDate' => $row['mailSendDate']
);
// $myMailData = json_encode($test);
// echo $myMailData;
return $test;
控制器:
public function index()
//$userLoginData = $this->session->userdata('user_login');
$data = array(
'sentFromEmail' => $this->session->userdata['user_login']['loginEmail'] ,
);
//load the method of model
$mailBoxData = array();
$mailBoxData['mailBoxData'] = $this->mail_receive->inbox($data);
$jsonData = json_encode($mailBoxData);
echo $jsonData;
$this->load->view('inbox', $mailBoxData);
查看文件:
<script>
$(document).ready(function()
$('#inbox').dataTable(
"processing": true,
"serverSide": true,
"ajax":
"url" : "http://localhost/codeigniter/index.php/Inbox_redirect/index",
"type" : "POST",
"dataSrc": ""
,
"columns" : [
"data" : "mailId",
"data" : "mailSender",
"data" : "mailSubject",
"data" : "mailContent",
"data" : "mailSendDate" ]
);
);
</script>
<table class="table table-hover table-striped" id="inbox" name="inbox">
<thead>
<th>ID</th>
<th>Sent By:</th>
<th>Time</th>
<th>Subject</th>
<th>Message</th>
</thead>
<div class="container">
<tbody>
</tbody>
</div>
</table>
如何纠正错误?
得到记录的json响应是
["mailId":"13","mailSender":"nikita","mailSubject":"testing","mailContent":"njcndncvjdvnfjvnfvnfjvnjkfnvkfnbkfkbnfdbteb","mailSendDate":"2016-11-16 15:04:20","mailId":"14","mailSender":"nikita","mailSubject":"testing","mailContent":"njcndncvjdvnfjvnfvnfjvnjkfnvkfnbkfkbnfdbteb","mailSendDate":"2016-11-16 15:23:02","mailId":"17","mailSender":"nikita","mailSubject":"wygdyegfhfbvhrvf","mailContent":"ghfgregughuthgujbhjhykhytj","mailSendDate":"2016-11-17 12:55:20","mailId":"21","mailSender":"jyotsna","mailSubject":"hi there","mailContent":"hello, how are you?","mailSendDate":"2016-11-18 14:50:56"]
【问题讨论】:
尝试不带键的数组,例如 $myMail[] = array('mailId' => $row['mailId'],'mailSender' => $columnSenderName,.....); to $myMail[] = array($row['mailId'],$columnSenderName .....); 它的用途是什么? json 响应被正确记录,但数据表调用无法获得有效的 json 响应。另外,没有键的数组是没有意义的 为什么还要打印 json 数据并加载视图?它混合了两种内容,使 json 无效。 我认为@phobia82 是正确的。你应该删除echo $jsonData;
那是检查我的模型是否正确处理数据,主要是为了测试目的
【参考方案1】:
尝试在里面添加“dataType”:
"ajax":
"url" : "http://localhost/codeigniter/index.php/Inbox_redirect/index",
"type" : "POST",
"dataSrc": ""
像这样:
"ajax":
"url" : "http://localhost/codeigniter/index.php/Inbox_redirect/index",
"type" : "POST",
"dataType": "json", // The type of data that you're expecting back from the server.
"dataSrc": ""
如果这不起作用,您仍然可以尝试解析得到的结果:JSON.parse('your JSON string');
。您从 PHP 发送的 JSON 字符串,使用 json_encode()
方法生成。
【讨论】:
解决方案是在控制器中我使用 echo json_encode($mailBoxData);那行得通。无论如何,谢谢!以上是关于jquery数据表中的无效json响应的主要内容,如果未能解决你的问题,请参考以下文章
jquery Datatable 无效 JSON 响应 1 ,数据被检查为有效 json