简体   繁体   English

DataTables警告:无法解析来自服务器的JSON数据。 这是由JSON格式错误引起的

[英]DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error

I am using server-side DataTables in a project 我在项目中使用服务器端DataTables

and do a search, shows me the following error message: 并进行搜索,显示以下错误消息:

DataTables warning: JSON data from server not could be parsed. This is Caused by a JSON formatting error.

I found some suggestions in the forums but nothing has worked for me 我在论坛上找到了一些建议,但没有任何对我有用

Anyone have any suggestions? 有人有什么建议吗?

检查firebug中的网络选项卡,您可能会看到底层服务器错误

I ran into this problem and it was as simple as updating the information in the following file: 我遇到了这个问题,就像更新以下文件中的信息一样简单:

    "sAjaxSource": "DataTables/examples/server_side/scripts/server_processing.php"

Don't know if the answer you're looking for is that pedestrian, but I figured I would throw in my 2 cents!! 不知道你所寻找的答案是否是行人,但我想我会投入2美分!

你的json不是数据表所期望的有效格式,即行/列2d数组。

[["row1col1","row1col2"],["row2col1"],.....]

For newer versions of datatables,don't forget to remove this line from the server_processing.php file 对于较新版本的数据表,请不要忘记从server_processing.php文件中删除此行

/* REMOVE THIS LINE (it just includes my SQL connection user/pass) */
    include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" );

If you are using : 如果您正在使用:

"sAjaxSource": "myHandler.php"

Then you mus return from your handler: 然后你从你的处理程序返回:

"{\"aaData\": [{\"propOne\": \"valueOne\"},{\"propTwo\": \"valueTwo\"}], [...],... }"

Note 注意

\"aaData\":

before json collection and note brakects: 在json收集和注意brakects之前:

{\"aaData\"....}

My example in asp.net(this is working): 我在asp.net中的例子(这是有效的):

Response.Write("{\"aaData\": [{\"propOne\": \"valueOne\"},{\"propTwo\": \"valueTwo\"}]}");

Client page: 客户页面:

      oTable = $('#myTable').dataTable( {
        "bProcessing": true,
        "sPaginationType": "full_numbers",
        "aLengthMenu": [[5,10,20,50,100], [5,10,20,50,100]],
        "aaSorting": [[ 1, "desc" ]],
        "sAjaxSource": "MyHandler.ashx",
        "aoColumns": [
            { "mDataProp": "propOne" },
            { "mDataProp": "propTwo" }
        ]
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM