2

我无法解决错误,我浏览了许多网站上给出的所有建议。任何建议都会非常有帮助。提前致谢。

jQuery代码:

$(document).ready(function () {
    var client = {
        "clientId": $('#clientId').val(),
            "firstName": $('#firstName').val(),
            "lastName": null
    };
    var jsonReqString = JSON.stringify(client, function (key, value) {
        if (typeof value == "number") return value;
        if (typeof value == "boolean") return value;
        return value == "" ? null : value;
    });
    $.ajax({
        url: "/SimpleDT/search",
        type: "POST",
        data: jsonReqString,
        dataType: "json",
        jsonp: "cspcb",
        contentType: "application/json",
        timeout: 60000,
        xhrFields: {
            withCredentials: false
        },
        beforeSend: function (xhrObj) {},
        success: function (retdata) {
            alert("retdata = " + retdata);
            //retdata ={"aaData": retdata};

            var result_string = JSON.stringify(retdata, function (key, value) {
                if (typeof value == "number") return value;
                if (typeof value == "boolean") return value;
                if (key == 'clientId' && value == "") return "123321";

                return value == "" ? null : value;
            });

            var objs = jQuery.parseJSON(result_string);

            console.log("result_string data=" + result_string);

            console.log("Obj=" + objs);

            var lang = "";
            $.each(objs, function () {
                lang += this['clientId'] + "--" + this['firstName'] + "--" + this['lastName'] + "--" + this['compName'] + "--" + this['fileNo'] + "--" + this['resiAddr1'] + "--" + this['resiAddr2'] + "--" + this['resiAddr3'] + "--" + this['city'] + "--" + this['pinCode'] + "--" + this['mobileNo1'] + "--" + this['mobileNo2'] + "--" + this['mobileNo3'] + "--" + this['submissionDate'] + "--" + this['reminderDate'];
                console.log(lang);
                lang = "";
            });
            $('#example').dataTable({
                /* "bProcessing": true,
                            "bServerSide": true,
                            "sAjaxSource": "/Spring3Hibernate4Annotation/search",
                            "sServerMethod": "POST", 
                                */
                    "aaData": "result_string",
                /*  "aoColumns" : [ {
                                "mDataProp" : "clientId", sDefaultContent: "n/a" 
                            }, {
                                "mDataProp" : "firstName", sDefaultContent: "n/a" 
                            }, {
                                "mDataProp" : "lastName", sDefaultContent: "n/a" 
                            }, {
                                "mDataProp" : "compName", sDefaultContent: "n/a" 
                            }, {
                                "mDataProp" : "fileNo", sDefaultContent: "n/a" 
                            } ] */

                    "aoColumns": [{
                    "mDataProp": "clientId"
                }, {
                    "mDataProp": "firstName"
                }, {
                    "mDataProp": "lastName"
                }, {
                    "mDataProp": "compName"
                }, {
                    "mDataProp": "fileNo"
                }]
            });

        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(
                "The following error occured in maintainance Query: " + textStatus, errorThrown);
        }
    });
});

HTML 代码:

<table cellpadding="0" cellspacing="0" border="0" class="display"
    id="example">
    <thead>
        <tr>
            <th width="20%">Client Id</th>
            <th width="25%">First Name</th>
            <th width="25%">Last Name</th>
            <th width="15%">Comp Name</th>
            <th width="15%">File No</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
    <tfoot>
        <tr>
            <th>Client Id</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Comp Name</th>
            <th>File No.</th>
        </tr>
    </tfoot>
</table>

示例:我从 spring 控制器收到的 JSON 响应:

[{
    "clientId": "12345",
        "firstName": "fgh",
        "lastName": "",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "1234",
        "firstName": "",
        "lastName": "",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "1234765",
        "firstName": "XXXXX",
        "lastName": "sdgg",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "2222222",
        "firstName": "dghdhfjfj",
        "lastName": "gsjdjkvbcb",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "2222222123",
        "firstName": "dghdhfjfj",
        "lastName": "gsjdjkvbcb",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "2312234412",
        "firstName": "fdsffsf",
        "lastName": "fsfggg",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "2313377",
        "firstName": "fdsffsf",
        "lastName": "fsfggg",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23133772",
        "firstName": "ASDFREW",
        "lastName": "Bandalu",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23133773",
        "firstName": "SASASAS",
        "lastName": "BDDFFDDDD",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23133774",
        "firstName": "SDASADDS",
        "lastName": "DFASDFF",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23133775",
        "firstName": "SDASDADD",
        "lastName": "DSADFFFDD",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23133776",
        "firstName": "ASSSSS",
        "lastName": "DDDSSSS",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23133777",
        "firstName": "LKJKLLKK",
        "lastName": "LPSLDFF",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23133778",
        "firstName": "ASDFREW",
        "lastName": "Bandalu",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "2313389",
        "firstName": "fdsffsf",
        "lastName": "fsfggg",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "2344",
        "firstName": "fdsffsf",
        "lastName": "fsfggg",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "234412",
        "firstName": "fdsffsf",
        "lastName": "fsfggg",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23441q2",
        "firstName": "fdsffsf",
        "lastName": "fsfggg",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "23456",
        "firstName": "Rajiv",
        "lastName": "Muddas",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "33333333",
        "firstName": "Jhon",
        "lastName": "Muddas",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "3333333333",
        "firstName": "RA",
        "lastName": "Gulit",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "43568",
        "firstName": "AFSF",
        "lastName": "ADFFGGSDF",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "444444444",
        "firstName": "Raju",
        "lastName": "cgdhh",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}, {
    "clientId": "55555",
        "firstName": "Jhon",
        "lastName": "Muddas",
        "compName": null,
        "fileNo": null,
        "resiAddr1": null,
        "resiAddr2": null,
        "resiAddr3": null,
        "city": null,
        "pinCode": null,
        "mobileNo1": null,
        "mobileNo2": null,
        "mobileNo3": null,
        "submissionDate": null,
        "reminderDate": null
}]
4

2 回答 2

4

我认为您需要在正文中放置 a trwith some tds,以便 DataTables 知道在哪里呈现输出:

<tbody>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</tbody>

th当我的元素数量与我td的 '相比不正确时,我也遇到了类似的错误,所以我认为这是相似的。

于 2013-11-04T16:22:48.133 回答
2

您正在设置aaData一个字符串"result_string",您想将其设置为对象retdata(至少它看起来像这样)。

"aaData": retdata,

aaData应该是数组的数组,但是对象数组可能会起作用。通常mDataProp在您使用sAjaxSource.

如果您使用过sAjaxSource,您可能可以执行以下操作而不是您的 ajax 调用

$('#example').dataTable({
      "sAjaxSource": "/SimpleDT/search",
      "aoColumns": [{ "mDataProp": "clientId"  }, 
                    { "mDataProp": "firstName" }, 
                    { "mDataProp": "lastName"  }, 
                    { "mDataProp": "compName"  }, 
                    { "mDataProp": "fileNo"    }],
      "fnServerData": function( sUrl, aoData, fnCallback, oSettings ) {
        oSettings.jqXHR = $.ajax( {
            url: sUrl,
            type: "POST",
            data: jsonReqString,
            dataType: "json",
            jsonp: "cspcb",
            contentType: "application/json",
            timeout: 60000,
            xhrFields: { withCredentials: false }
            cache: false
        });
      }
});
于 2013-11-04T16:26:34.693 回答