How to receive ajax.data in controller?

How to receive ajax.data in controller?

fregefrege Posts: 3Questions: 1Answers: 0

Hi, I'm using datatables with server side processing, but I need to receive an id in the controller in order to get the data from the DB. The call is the following:

<script type="text/javascript">
    $(document).ready( function () {
        alert("${yellowAlarm.id}");

        $('#actionTable').DataTable({
            "orderMulti":false,
            "bServerSide":true,
            "sAjaxSource":"/ajax/yaAction",
            "bJQueryUI":true,
            "bProcessing":true,
            "order": [[ 2, "desc" ]],
            "data": {
                "alarm_id": 25
            },
            "language": {
                "url": "/js/spanishDatatable.json"
            }
        });
    });
</script>

In the future, alarm_id will be a variable, but right now I only want to make it work. The controller is like this:

public void getActionList (String alarm_id, HttpServletRequest request, HttpServletResponse response) {
...
        String id1 = request.getParameter("alarm_id");
        System.out.println("\nalarmId: " + id1);
        System.out.println("\nalarmId2: " + alarm_id);

        Enumeration<String> keys = request.getParameterNames();
        while (keys.hasMoreElements()) {
            String s = keys.nextElement();
            System.out.println("\n key: " + s + " value: " + request.getParameterValues(s)[0].toString());
        }
...
}

The first two println give me null, and the loop to iterate over all parameters don't show the key alarm_id nor the value 25. I also tried putting @RequestParam before String alarm_id to no result and adding "type":"POST" too. Can anyone help me?

Thanks in advance,
Frege.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    edited August 2019 Answer ✓

    Are you using Datatables 1.10 or 1.9? Looks like 1.10 but you have a mixture of legacy options. I'm not familiar with using the 1.9 sAjaxSource option so if you are using 1.9 then my answer might not help and you will need to refer to the 1.9 docs:
    https://legacy.datatables.net/usage/

    If using 1.10 then replace the sAjaxSource option with the ajax option. With the ajax option you can use ajax.data to send your parameters to the server.

                "data": {
                    "alarm_id": 25
                },
    

    Where you have this placed in your code is actually the data option which defines the table columns. What you want is something more like this:

    $('#example').DataTable( {
      "ajax": {
        "url": "/ajax/yaAction",
        "data": {
            "alarm_id": 25
        }
      }
      // the remainder of you config options.
    } );
    

    Kevin

  • fregefrege Posts: 3Questions: 1Answers: 0
    edited August 2019

    Thanks for your answer Kevin.
    To try this, I have to know how to access that from the controller. Is it

    String id1 = request.getParameter("alarm_id");

    the way to do it?

    EDIT: I'm using 1.10.19, but the code is from an example and not directly from the docs.

  • fregefrege Posts: 3Questions: 1Answers: 0
    edited August 2019

    Hi again, I tried this:

                    <script type="text/javascript">
                        $(document).ready( function () {
                            $('#actionTable').dataTable({
                                "ajax": {
                                    "url": "/ajax/yaAction",
                                    "data": {
                                        "alarm_id": 25
                                    }
                                },
                                "orderMulti":false,
                                "bJQueryUI":true,
                                "bProcessing":true,
                                "order": [[ 2, "desc" ]],
                                "language": {
                                    "url": "/js/spanishDatatable.json"
                                }
                            });
                        });
                    </script>
    

    This works, but the params the server receives are the new ones and not the legacy ones, so I have to adapt the controller. If you want to know, if I use sAjaxSource legacy is activated and I have to delete the ajax block, and if I want full 1.10 then add ajax block and delete sAjaxSource.

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    You can use $.fn.dataTable.ext.legacy.ajax = true; if you really really want legacy parameters. More information on that is available here.

    Worth noting that the next major version of DataTables will remove them from the code base (although I'll probably do some kind of backwards compatibility layer - still undecided about that!).

    Allan

This discussion has been closed.