首页 > 解决方案 > DataTables 警告:table id=example - 从服务器分页时 JSON 响应无效

问题描述

这是我的桌子

<table id="example" class="display" style="width: 100%">
            <thead>
                <tr>
                    <th>latitude</th>
                    <th>longitude</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>latitude</th>
                    <th>longitude</th>
                </tr>
            </tfoot>
        </table>

这是我的脚本

 <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<script>
    $(document).ready(function () {
        $(document).ready(function () {
            $('#example').DataTable({
                "processing": true,
                "serverSide": true,
                "info": true,
                "stateSave": true,
                "lengthMenu": [[10, 20, 50, -1], [10, 20, 50, "All"]],
                "ajax": {
                    "url": "Default3.aspx/GetRouteName",
                    "type": "GET"
                },
                "columns": [
                    { "data": "latitude" },
                   { "data": "longitude" }
                ],
                "order": [[0, "asc"]]
            });
        });
    });
</script>

在页面的末尾,我正在加载以下脚本

<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

这是我的服务器

 [WebMethod]
public static string GetRouteName()
{
    List<location> b = new List<location>();
    for (int i = 0; i < 1000; i++)
    {
        location l = new location();
        l.latitude = 1233;
        l.longitude = 123123;
        b.Add(l);
    }
    bise bb = new bise();
    bb.data = b;
    bb.draw = 3;
    bb.recordsFiltered = 1000;
    bb.recordsTotal = 1000;
    return JsonConvert.SerializeObject(bb);
}

我不断得到DataTables warning: table id=example - Invalid JSON response我已经尝试了这么多周转但同样的问题,我所需要的只是能够从服务器分页数据表,因为我有大数据,但 ajax 调用甚至没有触发,请从服务器引用此链接数据表

标签: ajaxdatatables

解决方案


[WebMethod]
public static string GetRouteName()
{
    List<location> b = new List<location>();
    for (int i = 0; i < 1000; i++)
    {
        location l = new location();
        l.latitude = 1233;
        l.longitude = 123123;
        b.Add(l);
    }

    return JsonConvert.SerializeObject(b);
} 

你能试试这个吗?它在工作吗?


推荐阅读