2015-11-07 47 views
2

我使用jQuery的數據表,並得到這樣的警告消息:DataTables警告:table = userTable - 無效的JSON響應?

數據表警告:表=用戶表 - 無效JSON響應

一個servlet取從MySQL用戶,我想在jQuery的數據表顯示,但Ajax無法解析JSON或者JSON在servlet中生成錯誤?

的Servlet:

List<UserDTO> users = this.service.getAllUser(); 
       Gson gson = new Gson(); 
       request.setAttribute("users", gson.toJson(users)); 
       request.getRequestDispatcher("listAllUser.jsp").forward(request, response); 

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
     pageEncoding="ISO-8859-1"%> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Registered Users</title> 
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 
    <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
    <script 
     src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script> 
    <link rel="stylesheet" 
     href="http://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" /> 
    <link rel="stylesheet" 
     href="http://code.jquery.com/ui/1.11.4/themes/flick/jquery-ui.css"> 
    <script> 
     $(document).ready(function() { 
      $('#userTable').dataTable({ 
       "processing" : true, 
       "serverSide" : true, 
       "ajax" : { 
        "url" : "ListAllUserServlet", 
        "type" : "POST" 
       }, 
       "columns" : [ { 
        "data" : "id" 
       }, { 
        "data" : "userName" 
       }, { 
        "data" : "firstName" 
       }, { 
        "data" : "lastName" 
       }, { 
        "data" : "email" 
       }, { 
        "data" : "phone" 
       }, { 
        "data" : "location" 
       }, { 
        "data" : "password" 
       }, { 
        "data" : "gender" 
       }, { 
        "data" : "birthday" 
       } ] 
      }); 
     }); 
    </script> 

    </head> 
    <body> 
     <table id="userTable" class="display"> 
      <thead> 
       <tr> 
        <th colspan="10" id="userList">Users</th> 
       </tr> 
       <tr> 
        <th>User id</th> 
        <th>User name</th> 
        <th>First Name</th> 
        <th>Last Name</th> 
        <th>Email</th> 
        <th>Phone</th> 
        <th>Location</th> 
        <th>Password</th> 
        <th>Gender</th> 
        <th>Birth date</th> 
       </tr> 
      </thead> 

      <tfoot> 
       <tr> 
        <td colspan="10"><a href="index.jsp" id="toIndex">Back</a></td> 
       </tr> 
      </tfoot> 
     </table> 

    </body> 
    </html> 

JSON由servlet的產生:

[ 
    { 
     "id": 1, 
     "userName": "userName1", 
     "firstName": "firstName1", 
     "lastName": "lastName1", 
     "email": "[email protected]", 
     "phone": "36202080085", 
     "location": "location1", 
     "password": "password1", 
     "gender": "m", 
     "birthday": "1-02-2015" 
    } 
] 

回答

5

有你的代碼夫婦的問題:

  • 您已啓用服務器端處理模式"serverSide": true,但您的數據格式化爲客戶端處理模式。刪除"serverSide": true以使用客戶端處理模式。

  • 您需要使用下面顯示的dataSrc: ""灰以匹配您的JSON數據格式,請參閱dataSrc瞭解更多信息。

    "ajax" : { 
        "url" : "ListAllUserServlet", 
        "type" : "POST", 
        "dataSrc": "" 
    }, 
    
+0

刪除 「服務器端」 的:真正爲我工作! –