“Uncaught TypeError: Cannot use 'in' operator to search for 'length' in ” triggered by Datatables plugin and jQuery 1.11.3

后端 未结 8 1307
北恋
北恋 2021-02-12 13:17

I\'m using the jQuery Datatables plugin to enable pagination, sorting and searching with my tables. The elements are showing up but not working, and the pagination only sometime

8条回答
  •  没有蜡笔的小新
    2021-02-12 13:41

    I fixed a similar issue by adding the json dataType like so:

    $.ajax({
        type: "POST",
        url: "someUrl",
        dataType: "json",
        data: {
            varname1 : "varvalue1",
            varname2 : "varvalue2"
        },
        success: function (data) {
            $.each(data, function (varname, varvalue){
                ...
            });  
        }
    });
    

    And in my controller I had to use double quotes around any strings like so (note: they have to be escaped in java):

    @RequestMapping(value = "/someUrl", method=RequestMethod.POST)
    @ResponseBody
    public String getJsonData(@RequestBody String parameters) {
        // parameters = varname1=varvalue1&varname2=varvalue2
        String exampleData = "{\"somename1\":\"somevalue1\",\"somename2\":\"somevalue2\"}";
        return exampleData;
    }
    

提交回复
热议问题