[英]Invalid json response in jquery datatable
I have tried jquery datatables for the first time. 我第一次尝试了jquery数据表。 After referring the manual etc I was able to write the program below, but I am constantly getting an error stating that the json response is invalid. 在参考手册等之后,我能够在下面编写程序,但是我不断收到错误消息,指出json响应无效。
model file: 模型文件:
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;
}
controller: 控制器:
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);
}
view file: 查看文件:
<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>
How do I rectify the error? 如何纠正错误?
the json response that getting recorded is 被记录的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"}]
Try to add "dataType" inside: 尝试在其中添加“ dataType”:
"ajax": {
"url" : "http://localhost/codeigniter/index.php/Inbox_redirect/index",
"type" : "POST",
"dataSrc": ""
}
like this: 像这样:
"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": ""
}
If this won't work, you can still try to parse the result which you get like this: JSON.parse('your JSON string');
如果这行不通,您仍然可以尝试解析这样的结果: JSON.parse('your JSON string');
. 。 The JSON string which you send from PHP, made with the json_encode()
method. 您从PHP发送的JSON字符串,使用json_encode()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.