POST vs sAjaxSource":

POST vs sAjaxSource":

jimbooth97jimbooth97 Posts: 5Questions: 0Answers: 0
edited July 2013 in General
I have gotten DataTables to work perfectly when I used sAjaxSource as what I needed was a database dump into the tables. But now i need to search for certain criteria that is dynamically driven by the user. In the past I used POST to send the data to search the database on (before using DataTables). The below javascript works good for the database dump, is there an easy way to pass along a few variables to only return certain information? I apologize in advance.. as this is a bit new to me.

$(document).ready(function() {
var oTable = $('#example').dataTable( {
"bPaginate": true,
"bProcessing": true,
"bAutoWidth": false,
"bServerSide": true,
"bSortable_3": false,

"sAjaxSource": "tripsgps.php"
} );

} );

Replies

  • jimbooth97jimbooth97 Posts: 5Questions: 0Answers: 0
    Looks like this is what need..


    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/server_processing.php",
    "fnServerParams": function ( aoData ) {
    aoData.push( { "name": "more_data", "value": "my_value" } );
    }
    } );
    } );
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Also you can use sAjaxMethod to tell DataTables to use POST rather then GET if you want., But yes, fnServerParams is currently the way to add more data to the request parameters.

    Allan
  • jimbooth97jimbooth97 Posts: 5Questions: 0Answers: 0
    Hi Allan, is there an example of that technique?
  • jimbooth97jimbooth97 Posts: 5Questions: 0Answers: 0
    i found this... notice i modified it to my use.. ie i removed the quotes on the variable for the three variables i want to send. I commented out the oPostData since I think that is just adding a new variable to the oPostData array. Am I way off base?

    $.ajaxSetup( {'type': 'POST', 'url':'tripsgps.php', 'dataType': 'json' } );

    var oPostData = {'user':username, 'vehicle':uservehicle, 'regnumber':userregnumber};

    //oPostData['name3'] = this.options.value3;

    $('#example').datatable({

    'sAjaxSource': $.ajaxSettings.url,

    'aoAjaxData':oPostData

    });
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Sorry - I should have said sServerMethod ! :-(.

    All you need to do is add `sServerMethod: 'POST'` to the DataTables initialisation:
    http://datatables.net/release-datatables/examples/server_side/post.html

    Allan
  • jimbooth97jimbooth97 Posts: 5Questions: 0Answers: 0
    After looking at the example, I don't see where I put the data (variables).. maybe soemthing like this?

    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/post.php", data: {hognum:hognum, vehicle:vehicle},
    "sServerMethod": "POST"
    } );
    } );
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    You'd use fnServerParams just like you did above :-)

    Allan
This discussion has been closed.