Javascript 如何通过ajax向服务器(Java)发送变量,然后加载带有响应的数据表

Javascript 如何通过ajax向服务器(Java)发送变量,然后加载带有响应的数据表,javascript,java,jquery,ajax,datatables,Javascript,Java,Jquery,Ajax,Datatables,JavaScript $(document).ready(function() { console.log("get tasks"); $('#tblTask').dataTable({ <!-- Retrieve a static json file, you could also have a URL to a controller method. --> "sAjaxSource" : "/getTasks", "

JavaScript

$(document).ready(function() {
    console.log("get tasks");
    $('#tblTask').dataTable({
        <!-- Retrieve a static json file, you could also have a URL to a controller method. -->
        "sAjaxSource" : "/getTasks",
        "sAjaxDataProp": "",
        <!-- Indicate to dataTable what the field names are we want, in the order we want them in the table. -->
        "aoColumns": [
                      {"data": "taskID",
                         "visible": false},
                      {"data": "task_Runbook_ID",
                         "visible": false},
                      {"data": "taskNumber"},
                      {"data": "taskDependencies"},
                      {"data": "taskStatus"},
                      {"data": "taskDescription"},
                      {"data": "duration"},
                      {"data": "ownerName"},
                      {"data": "planStartTime"},
                      {"data": "planEndTime"},
                      {"data": "actualStartTime"},
                      {"data": "actualEndTime"},
                      {"data": "comments"}
            ]


    });
});
我想出来了

"sAjaxSource" : "/getTasks",
            "fnServerData": function ( sSource, aoData, fnCallback ) {
                /* Add some data to send to the source */
                aoData.push( { "name": "rbID", "value": rbID } );
                $.ajax( {
                    "dataType": 'json',
                    "url": sSource,
                    "data": aoData,
                    "success": fnCallback
                } ); },
@RequestMapping(value = "/getTasks")
public @ResponseBody Iterable<Task> getTasks(Model model) {
    //  System.out.println("RBID!!!" + request.getParameter("rbID"));
    List<Task> tasks = taskRepository.findByTaskRunbookID(3); //3 is here until we can get id from gui
    return tasks;
}
var rbID = something;

$('#tblTask').dataTable({
        "sAjaxSource" : "/getTasks",
        "data" : {"rbID" : rbID }, //this is sent to the server, where I query a list of tasks where taskID is equal to this rbID.

        "sAjaxDataProp": "",
        "aoColumns": [.....] // Then the response is a JSON object, which I display in the DataTable like so.
"sAjaxSource" : "/getTasks",
            "fnServerData": function ( sSource, aoData, fnCallback ) {
                /* Add some data to send to the source */
                aoData.push( { "name": "rbID", "value": rbID } );
                $.ajax( {
                    "dataType": 'json',
                    "url": sSource,
                    "data": aoData,
                    "success": fnCallback
                } ); },