How to post the parameter in ajax call of jquery d

2019-04-04 01:43发布

问题:

As of now I am passing parameter along with URL in ajax call of data table.

But I want to pass it as POST method, please anyone one help me regarding parameter passing in post method, here's my trial code:

// Sending through GET
var $table = $('#example').dataTable( 
    "processing": true,
    "serverSide": true,
    "bDestroy": true,
    "bJQueryUI": true,
    "ajax": 'getResult.php?formName=afscpMcn&action=search&mcn_no='+mcnNum+'&cust_nm='+cust_num+'&emp_id='+emp+''
});

回答1:

Just pass it like a normal jQuery ajax in POST fashion.

The structure should look like this:

ajax: { type: 'POST', url: <path>, data: { your desired data } }

Example:

var $table = $('#example').dataTable( 
    "processing": true,
    "serverSide": true,
    "bDestroy": true,
    "bJQueryUI": true,
    "ajax": {
        'type': 'POST',
        'url': 'getResult.php',
        'data': {
           formName: 'afscpMcn',
           action: 'search',
           // etc..
        },
    }
});

In PHP, just access the POST indices as usual (just the straightforward approach):

getResult.php

$form_name = $_POST['formName'];
// the rest of your values ...

DataTables manual entry



回答2:

$("#tbl").dataTable({
            oLanguage: {
                sProcessing: '<div id="loader"></div>'
            },
            bProcessing: true,
            "bServerSide": true,
            "iDisplayLength": pageSize,
            "sAjaxSource": " /PurchaseOrder/AddVendorItems", // url getData.php etc
            "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
                aoData.push({ "name": "where", "value": ID +" AND ISNULL(IsFinal,0) = "+ ($("#chkFinal").bootstrapSwitch('state') == true ? 1 : 0) });
                aoData.push({"name": "PackIDFK", "value": $("#PackIDFK").val()}) //pushing custom parameters
                oSettings.jqXHR = $.ajax( {
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": fnCallback
                } );
            } });

This is real time example.The aoData contains all the parameters which is required on server side and you can also push your own custom parameters