jquery - 数据表警告 : table=userTable - Invalid JSON response?

标签 jquery jsp servlets datatables gson

我使用 jQuery DataTables 并收到此警告消息:

DataTables warning: table=userTable - Invalid JSON response

servlet 从 MySQL 获取用户,我想将其显示在 jQuery 数据表中,但 Ajax 无法解析 JSON 或 servlet 中生成的 JSON 错误?

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>

servlet 生成的 JSON:

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

最佳答案

您的代码存在几个问题:

  • 您已使用 "serverSide": true 启用服务器端处理模式,但您的数据格式改为客户端处理模式。删除 "serverSide": true 以使用客户端处理模式。

  • 您需要使用如下所示的 dataSrc: "" ash 来匹配您的 JSON 数据格式,请参阅 dataSrc了解更多信息。

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

关于jquery - 数据表警告 : table=userTable - Invalid JSON response?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33582203/

相关文章:

c# - 如何像Stackoverflow一样制作消息通知?

javascript - 以文本作为掩码的 CSS div

java - Liferay 获取 URL 参数问题

java - ServletContext 变量不保留值

javascript - 将字符串转换为日期

javascript - 获取 FancyTree 中选定节点的父节点

java - 在 Java 中获取 <td> 的值 - request.getParameter

java - 通过 jsp (SMTP EXIM) 发送电子邮件时出错

java - web片段和web.xml中servlet监听器的执行顺序

javascript - 使用 XMLHttpRequest 处理参数中的特殊字符