簡體   English   中英

jQuery DataTable中的無效JSON響應

[英]Invalid json response in jquery datatable

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

嘗試在其中添加“ 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()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM