我第一次尝试了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"}]