sServerMethod: POST and overriding fnServerData do not work

sServerMethod: POST and overriding fnServerData do not work

ericDeckerericDecker Posts: 15Questions: 0Answers: 0
edited September 2013 in General
Hello,

I'm having trouble switching one of my Datatables from GET to POST. The table works in the default GET format, but as soon as I change it to use POST, the Datatable never gets populated, despite valid JSON being returned. I'm including a debug link, but unfortunately I cannot provide a test environment. I'm using the latest version of Datatables and I'm not getting any kind of error output in Firebug. Everything looks fine when I inspect the network traffic, so I'd really appreciate any advice that anyone could offer.

http://debug.datatables.net/atomop

Replies

  • allanallan Posts: 63,386Questions: 1Answers: 10,449 Site admin
    It looks like the fnServerData method hasn't been overridden in your debug link there, but fundamentally, if you provide your own Ajax method in fnServerData, then sServerMethod has no effect since you are providing your own Ajax method! If you want POST, set it in your Ajax get.

    Allan
  • ericDeckerericDecker Posts: 15Questions: 0Answers: 0
    edited September 2013
    Hi Allan,

    Sorry for not being clear. I am testing the methods uniquely, but didn't think it would be worth posting a link to both versions.

    Here is the code that I am trying to use in the function:
    [code]"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
    oSettings.jqXHR = $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    } );
    }[/code]

    I also tried it this way:
    [code]"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
    oSettings.jqXHR = $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": function(result){ fnCallback(result); }
    } );
    }[/code]

    Both cases produce the same problem: the data is not being inserted to the Datatable. Am I not correctly setting it to post as you suggest?
  • allanallan Posts: 63,386Questions: 1Answers: 10,449 Site admin
    That looks perfectly fine to me - whatever is in aoData should be sent to the server. Try `console.log( aoData )` just to make sure that there is data in it!

    Allan
  • ericDeckerericDecker Posts: 15Questions: 0Answers: 0
    Allan, I appreciate your advice, but I know that is not the issue. My server side script is returning the data, including filtered searches, it's simply not appearing in the Datatable.

    Here is what my aoData reads:
    [code][Object { name="sEcho", value=1}, Object { name="iColumns", value=20}, Object { name="sColumns", value=""}, Object { name="iDisplayStart", value=0}, Object { name="iDisplayLength", value=50}, Object { name="mDataProp_0", value="LEAD"}, Object { name="mDataProp_1", value="TYPE"}, Object { name="mDataProp_2", value="STATUS"}, Object { name="mDataProp_3", value="CHANGE_CONTROL"}, Object { name="mDataProp_4", value="APPLICATION_NAME"}, Object { name="mDataProp_5", value="QUESTION"}, Object { name="mDataProp_6", value="NOTIFICATION_DATE"}, Object { name="mDataProp_7", value="RESPONSE"}, Object { name="mDataProp_8", value="RESPONSE_SUBMIT_DATE"}, Object { name="mDataProp_9", value="RESPONSE_APPROVAL_DATE"}, Object { name="mDataProp_10", value="PROD_NAME"}, Object { name="mDataProp_11", value="ACTIVE_INGREDIENT"}, Object { name="mDataProp_12", value="STRENGTH"}, Object { name="mDataProp_13", value="FORM"}, Object { name="mDataProp_14", value="DOSSIER"}, Object { name="mDataProp_15", value="SECTION"}, Object { name="mDataProp_16", value="COUNTRY"}, Object { name="mDataProp_17", value="FILE_COUNT"}, Object { name="mDataProp_18", value="COMMENT_COUNT"}, Object { name="mDataProp_19", value="FOLLOWUP_COUNT"}, Object { name="sSearch", value=""}, Object { name="bRegex", value=false}, Object { name="sSearch_0", value=""}, Object { name="bRegex_0", value=false}, Object { name="bSearchable_0", value=true}, Object { name="sSearch_1", value=""}, Object { name="bRegex_1", value=false}, Object { name="bSearchable_1", value=true}, Object { name="sSearch_2", value=""}, Object { name="bRegex_2", value=false}, Object { name="bSearchable_2", value=true}, Object { name="sSearch_3", value=""}, Object { name="bRegex_3", value=false}, Object { name="bSearchable_3", value=true}, Object { name="sSearch_4", value=""}, Object { name="bRegex_4", value=false}, Object { name="bSearchable_4", value=true}, Object { name="sSearch_5", value=""}, Object { name="bRegex_5", value=false}, Object { name="bSearchable_5", value=true}, Object { name="sSearch_6", value=""}, Object { name="bRegex_6", value=false}, Object { name="bSearchable_6", value=true}, Object { name="sSearch_7", value=""}, Object { name="bRegex_7", value=false}, Object { name="bSearchable_7", value=true}, Object { name="sSearch_8", value=""}, Object { name="bRegex_8", value=false}, Object { name="bSearchable_8", value=true}, Object { name="sSearch_9", value=""}, Object { name="bRegex_9", value=false}, Object { name="bSearchable_9", value=true}, Object { name="sSearch_10", value=""}, Object { name="bRegex_10", value=false}, Object { name="bSearchable_10", value=true}, Object { name="sSearch_11", value=""}, Object { name="bRegex_11", value=false}, Object { name="bSearchable_11", value=true}, Object { name="sSearch_12", value=""}, Object { name="bRegex_12", value=false}, Object { name="bSearchable_12", value=true}, Object { name="sSearch_13", value=""}, Object { name="bRegex_13", value=false}, Object { name="bSearchable_13", value=true}, Object { name="sSearch_14", value=""}, Object { name="bRegex_14", value=false}, Object { name="bSearchable_14", value=true}, Object { name="sSearch_15", value=""}, Object { name="bRegex_15", value=false}, Object { name="bSearchable_15", value=true}, Object { name="sSearch_16", value=""}, Object { name="bRegex_16", value=false}, Object { name="bSearchable_16", value=true}, Object { name="sSearch_17", value=""}, Object { name="bRegex_17", value=false}, Object { name="bSearchable_17", value=true}, Object { name="sSearch_18", value=""}, Object { name="bRegex_18", value=false}, Object { name="bSearchable_18", value=true}, Object { name="sSearch_19", value=""}, Object { name="bRegex_19", value=false}, Object { name="bSearchable_19", value=true}, Object { name="iSortCol_0", value=6}, Object { name="sSortDir_0", value="desc"}, Object { name="iSortingCols", value=1}, Object { name="bSortable_0", value=true}, Object { name="bSortable_1", value=true}, Object { name="bSortable_2", value=true}, Object { name="bSortable_3", value=true}, Object { name="bSortable_4", value=true}, Object { name="bSortable_5", value=true}, Object { name="bSortable_6", value=true}, Object { name="bSortable_7", value=true}, Object { name="bSortable_8", value=true}, Object { name="bSortable_9", value=true}, Object { name="bSortable_10", value=true}, Object { name="bSortable_11", value=true}, Object { name="bSortable_12", value=true}, Object { name="bSortable_13", value=true}, Object { name="bSortable_14", value=true}, Object { name="bSortable_15", value=true}, Object { name="bSortable_16", value=true}, Object { name="bSortable_17", value=true}, Object { name="bSortable_18", value=true}, Object { name="bSortable_19", value=true}][/code]
  • allanallan Posts: 63,386Questions: 1Answers: 10,449 Site admin
    Ah - I understand now!

    > "sEcho": 0,

    in your debug trace. It should never be 0. It will be 1 and then increment on every draw. That's probably the issue (without having a test case to see directly).

    Allan
  • ericDeckerericDecker Posts: 15Questions: 0Answers: 0
    edited September 2013
    You're right, my script was looking for $_GET['sEcho']. Thanks for your help, Allan.
  • ericDeckerericDecker Posts: 15Questions: 0Answers: 0
    Allan,

    I'm running into trouble adding custom POST parameters to my request. I used the following code which I found in one of your old posts:
    [code]aoData.push({'user_id':'<?php echo $_SESSION['project-name-userID']['userId'];?>','type':'<?php echo $TYPE;?>','ID':'<?php echo $ID;?>','session_id':'<?php echo $_SESSION['project-name-userID']['session_id'];?>'});[/code]

    But when I inspect my parameters in the Firebug console, I only see the default Datatables parameters and one additional one named and valued "undefined." Can you please advise me on what I'm doing incorrectly?
  • allanallan Posts: 63,386Questions: 1Answers: 10,449 Site admin
    Use a name value pair like in the example here (i.e. have "name" and "value" as parameters): http://datatables.net/release-datatables/examples/server_side/custom_vars.html

    Allan
  • ericDeckerericDecker Posts: 15Questions: 0Answers: 0
    Allan, please tell me how the code I posted does not conform to the name value pair example you posted. Do you see any indices in my code that are invalid? Thanks for the link, but I still have no idea what I'm doing that I shouldn't be. (or what I'm not doing that I should be, I guess?)
  • ericDeckerericDecker Posts: 15Questions: 0Answers: 0
    When I copy and paste the values from the example, I expect to see two parameters, but somehow, all I can view in Firebug's console is a single parameter taking the first one's value as a name, and using the second one's value as the value...
  • ericDeckerericDecker Posts: 15Questions: 0Answers: 0
    When I log aoData before and after adding my parameters, I can see that they are successfully being added into the array, yet my PHP script is still being reported to receive undefined rather than my actual parameters...
  • allanallan Posts: 63,386Questions: 1Answers: 10,449 Site admin
    Try:

    [code]
    aoData.push({ name: 'user_id', value: '<?php echo $_SESSION['project-name-userID']['userId'];?>' );
    aoData.push({ name: 'type', value: '<?php echo $TYPE;?>' );
    aoData.push({ name: 'ID', value: '<?php echo $ID;?>' );
    aoData.push({ name: 'session_id', value: '<?php echo $_SESSION['project-name-userID']['session_id'];?>' );
    [/code]

    i.e. name value pairs like the example and documentation.

    If that doesn't work, please link to a test case so I can debug it.

    Allan
  • ericDeckerericDecker Posts: 15Questions: 0Answers: 0
    Oh, I see now, thanks for your help.
This discussion has been closed.