24

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+''
});
3
  • did you see this page? datatables.net/examples/server_side/post.html Commented Aug 26, 2014 at 12:25
  • just read the manual Commented Aug 26, 2014 at 12:25
  • I want to pass the parameter like form name ,action and other parameters.. In that manual they have mention only column data Commented Aug 26, 2014 at 12:28

3 Answers 3

56

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Kevin, I am having trouble when the json return 0 results, imagine if the table has no entries... so when it returns a blank JSON I am getting error from datatable.. it is a default alert informing the JSON is in wrong format.. Do you know how can I deal with it?
@Sophie it's important that whatever you have in your response is still a valid structure that's acceptable to the format datatables need when you return it to client side. datatables.net/examples/server_side/simple.html even if it's zero rows.
10

You can try this way:

$('#example').dataTable( {
  "ajax": {
    "url": "data.json",
    "data": function ( d ) {
        d.extra_search = $('#extra').val();
    }
  }
});

https://datatables.net/reference/option/ajax.data

Comments

1
$("#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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.