json 数据表错误:“请求未知参数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11472075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:26:44  来源:igfitidea点击:

DataTables Error: "Requested unknown parameter"

jsonspring-mvcHymansonjquery-datatables

提问by Steve

I'm new to the DataTables jquery plugin. After discovering that IE 8 had performance issues with Javascript I decided to change the way I use DataTables to do server side processing. I'm getting this error message when my JSP loads ( I'm using Spring 3 ):

我是 DataTables jquery 插件的新手。在发现 IE 8 的 Javascript 存在性能问题后,我决定改变使用 DataTables 进行服务器端处理的方式。我在加载 JSP 时收到此错误消息(我使用的是 Spring 3):

DataTables warning (table id = 'results_table'): Requested unknown parameter '0' from the data source for row 0

I Googled around and found that many causes of that error message come down to malformed JSON so I found a way to output my JSON from my Spring 3 controller function to take a look at the JSON it makes and I changed my code to get it to be pretty close to what the DataTablessite says it should look like.

我在谷歌上搜索,发现该错误消息的许多原因归结为格式错误的 JSON,所以我找到了一种从 Spring 3 控制器函数输出我的 JSON 的方法,以查看它生成的 JSON,我更改了代码以获取它非常接近DataTables网站所说的它应该是什么样子。

Still no joy, still getting that error message.

仍然没有喜悦,仍然收到该错误消息。

The server side processing examples I found for DataTables didn't include code for specifying the columns used on the client side, so I assumed I don't need it. Do I?

我为 DataTables 找到的服务器端处理示例不包含用于指定客户端使用的列的代码,所以我假设我不需要它。我吗?

Here are the relevant parts of my results.jsp:

以下是我的 results.jsp 的相关部分:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title>ACME: search results in a nice DataTables.net Plugin</title>
</head>
<body>

<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css" />
<script language = "JavaScript" type = "text/javascript" src = "../nsd/js/jquery-1.7.js"></script>
<script language = "JavaScript" type = "text/javascript" src = "../nsd/js/jquery.dataTables.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    $('#results_table').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sScrollX": "600px",
        "sServerMethod": "POST",
        "sAjaxSource": "/acme/resultstable",
    } );
} );
</script>


<form id="command" name="f" action="employee" method="post">

    <div id = "results">
        <table id = "results_table">
            <thead>           
                <tr>
                    <th>&nbsp;</th>
                    <th>ID</th>
                    <th>NO_PRINT</th>
                    <th>Full Name</th>
                    <th>Email Address</th>
                    <th>Phone Number</th>
                    <th>Organization</th>
                    <th>Organization Code</th>
                    <th>Position</th>
                    <th>Employee Type</th>
                </tr>
            </thead>
            <tbody>           
            </tbody>
        </table>

    </body>
</html>

Here is the JSON response I've been sending to it:

这是我发送给它的 JSON 响应:

{
  "sEcho" : 1,
  "iTotalRecords" : 1,
  "iTotalDisplayRecords" : 1,
  "aaData" : [ {
    "person_id" : "888888",
    "ID" : "999999",
    "no_print" : "&nbsp;",
    "fullname" : "Obama, Willard",
    "email_address" : "<a href = \"mailto:[email protected]\">[email protected]</a>",
    "current_phone_number" : "303-867-5309",
    "title" : "&nbsp;",
    "office" : "&nbsp;",
    "position" : "Contractor",
    "empl_code" : "CONT"
  } ]
}

Here is my Spring controller function I am using to send the JSON response via Hymanson. This includes code to output my JSON so I can see what it looks like. Could the JSON it outputs to stdout and what I am sending back to DataTables be different?

这是我用来通过 Hymanson 发送 JSON 响应的 Spring 控制器函数。这包括输出我的 JSON 的代码,所以我可以看到它的样子。它输出到 stdout 的 JSON 和我发送回 DataTables 的内容可能不同吗?

@RequestMapping(value = "/resultstable", method = RequestMethod.POST)
public @ResponseBody LinkedHashMap resultstable(ModelMap model,                 
                                                HttpSession session,
                                                @RequestParam (required=true) int sEcho,   
                                                @RequestParam (required=true) int iDisplayStart,   
                                                @RequestParam (required=true) int iDisplayLength,    
                                                @RequestParam (required=true) int iColumns,
                                                @RequestParam (required=true) int iSortCol_0, 
                                                @RequestParam (required=false)String sSortDir_0,
                                                @RequestParam (required=true) String sSearch ) {

    /*
    **********************************************************************
    **  These come from the DataTables.net Jquery plugin on results.jsp
    **********************************************************************
    **  sEcho,          -  just send it back, used by DataTables for synching
    **  iDisplayStart   -  index of the record to start with, ie 3 for the 3rd of 100 records
    **  iDisplayLength  -  number of records to send back starting with iDisplayStart  
    **  iColumns        -  number of columns to be displayed in the table
    **  iSortCol_0      -  the number of thee column to be sorted on
    **  sSortDir_0      -  direction of sorting: asc or desc
    **  sSearch         -  from the search box, filter results further on this term 
    ********************************************************************** 
    */

    String nextView                   = "results";
    String usertype                   = (String)session.getAttribute("usertype");
    Search search                     = new Search(usertype);
    List<LinkedHashMap> records       = null;
    String results                    = null;
    int number_of_records             = (Integer)session.getAttribute("number_of_records_found");
    ResultsView rv                    = new ResultsView();
    ResultsScreenTableHolder rstrh    = null;
    SearchScreenDataHolder ssdh2      = (SearchScreenDataHolder)session.getAttribute("search_screen_data_holder");
    ObjectMapper mapper               = new ObjectMapper();

    logger.debug("started");

    logger.debug("sEcho,         == " + sEcho         );
    logger.debug("iDisplayStart  == " + iDisplayStart  );
    logger.debug("iDisplayLength == " + iDisplayLength );
    logger.debug("iColumns       == " + iColumns       );
    logger.debug("iSortCol_0     == " + iSortCol_0     );
    logger.debug("sSortDir_0     == " + sSortDir_0     );
    logger.debug("sSearch        == " + sSearch        );


    try {
        records = search.searchForAnEmployee(ssdh2,usertype,sSearch,"asc",
                                             iSortCol_0,iDisplayStart, 
                                             iDisplayLength);    


        LinkedHashMap lhm= new java.util.LinkedHashMap();
        lhm.put("sEcho", sEcho);
        lhm.put("iTotalRecords",number_of_records);
        lhm.put("iTotalDisplayRecords",9);
        lhm.put("aaData",records);

        // convert user object to json string, and save to a file
        mapper.writeValue(new File("c:\Downloads\rstrh.json.txt"), lhm);

        // display to console
        logger.debug("My JSON: " + mapper.defaultPrettyPrintingWriter().writeValueAsString(lhm));

    }
    catch (Exception e) {
        logger.debug("\n",e);
    }

    return lhm;       

}// end function 

采纳答案by bah

I was having this same problem this morning. You need to have the aoColumnsparameter and use mDataPropAs in this:

今天早上我遇到了同样的问题。您需要拥有aoColumns参数并mDataProp在此使用As :

https://gist.github.com/1660712

https://gist.github.com/1660712

At least it solved my problem.

至少它解决了我的问题。

回答by joshgo

I ran into the same warning as well, but the cause was different. I had nullvalues in my data. The JSON format was correct, but DataTables does not know have a default rule for displaying nulls. The solution was to use the sDefaultContentproperty.

我也遇到了同样的警告,但原因不同。我只好在我的数据值。JSON 格式是正确的,但 DataTables 不知道显示nulls的默认规则。解决方案是使用sDefaultContent属性。

Sample aaData:

示例 aaData:

aaData: [
  { "Field1": "Foo", "Field2":null },
  { "Field1": "Bar", "Field2":null },
]

And then on the aoColumns, you can use the property as follows:

然后在 aoColumns 上,您可以按如下方式使用该属性:

aoColumns: [
  { "mData": "Field1", sDefaultContent: "n/a" },
  { "mData": "Field2", sDefaultContent: "" }
]

This is not your current problem, but you may encounter this issue in the future.

这不是您当前的问题,但您将来可能会遇到此问题。

Hope this was helpful.

希望这是有帮助的。

回答by Swetha

sDefaultContent option prevents displaying alert boxes only. Data tables wil not find the rule for displaying null values.

sDefaultContent 选项阻止只显示警告框。数据表找不到显示空值的规则。

Changing null values in the table will eliminate this warning..

更改表中的空值将消除此警告..

回答by abbaf33f

If it helps anyone I had a similar error because I had periods in my mRender function name:
My.Namespace.MyFunction(data, row);

如果它可以帮助任何人,我会遇到类似的错误,因为我的 mRender 函数名称中有句点:
My.Namespace.MyFunction(data, row);

This line:
var a = _fnSplitObjNotation( src );

这一行:
var a = _fnSplitObjNotation( src );

Splits that into separate objects, which obviously generates an error.

将其拆分为单独的对象,这显然会产生错误。

Using

使用

My_Namespace_MyFunction(data, row);

My_Namespace_MyFunction(data, row);

Additionally I noticed this error when passing a string function name instead of the JavaScript function object.

另外,我在传递字符串函数名称而不是 JavaScript 函数对象时注意到了这个错误。

回答by Muhammad Zeshan Ghafoor

To avoid this error, your table number of "th" columns should equal to returning data columns(in numbers), in the above problem it is aaData.

为避免此错误,您的“第”列的表数应等于返回数据列(以数字为单位),在上述问题中它是 aaData。

"aaData" : [ 
  [
   "person_id" : "888888",
   "ID" : "999999",
  ],
  [
   "person_id" : "8888889",
   "ID" : "9999990",
  ]
]

This is correct format to return data from server side language. I have solved my problem in same way.

这是从服务器端语言返回数据的正确格式。我已经以同样的方式解决了我的问题。