如何在post方法中传递params用于datatable ajax调用

时间:2018-05-28 06:24:38

标签: jquery ajax datatable

我需要在post方法中传递jquery ajax调用的所有参数。我正在解释下面的现有代码。

 var activeSemster = $('#tblActiveSemester').DataTable();
 activeSemster.ajax.url(
   "semesterSubjectMap_db.php?type=SELECT_ACTIVE_SEMESTER&_s=" +
   session + "&course_code=" + $('#cmbActvSemCourse').val() +
   "&batch_code=&semester_status=").load();

这里我传递了get方法中的所有值,我需要在post方法中传递这些参数。

2 个答案:

答案 0 :(得分:0)

您可以在fnServerData中传递自定义参数:

activeSemster = $('#tblActiveSemester').DataTable({
  "serverSide": true,
  "sAjaxSource": "semesterSubjectMap_db.php",
  "fnServerData": function(sSource, aoData, fnCallback, oSettings) {
    aoData.push({"name": "type", "value": "SELECT_ACTIVE_SEMESTER"});
    aoData.push({"name": "_s", "value": session});
    aoData.push({"name": "course_code", "value": $('#cmbActvSemCourse').val()});
    // ..etc
    $.ajax({
      "dataType": 'json',
      url: sSource,
      type: "post",
      data: aoData,
      success: fnCallback
    });
  }
});

答案 1 :(得分:0)

嗨,这段代码对我有用,

var tasks_table = jQuery('#tasks-tab-table').DataTable({
    "ajax": {
        url: ajax_url,
        "data": function (d) {
            d.action = "getTaskTabToDoTabTableData";
            d.post_per_page = tasks_post_per_page;
        },
        "error": function (jqXHR, exception) {
            if (jqXHR.status == 500) {
                alert('Internal Server Error [500]. Try after some time!');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        },
        "success": function (res) {
            var tasks_todo_table = jQuery('#tasks-tab-table').DataTable();
            tasks_todo_table.rows.add(res.data).draw(true);
            //your code
         },
         "error": function (jqXHR, exception) {
                    if (jqXHR.status == 500) {
                        console.log('Internal Server Error [500]. Try after some time!');
                    } else if (exception === 'timeout') {
                        console.log('Time out error.');
                    } else if (exception === 'abort') {
                        console.log('Ajax request aborted.');
                    } else {
                        console.log('Uncaught Error.\n' + jqXHR.responseText);
                    }
                },
            });
            //set total tasks and email pages - close
        }
    },
    "sDom": "ltp",
    "aaSorting": [1, 'desc'],
    "aoColumnDefs": [{"bSortable": false, "aTargets": [0, 2, 3, 4, 5, 6]}],
    "paging": false,
});

希望这会对你有所帮助。