How to receive ajax.data in controller?
How to receive ajax.data in controller?

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
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 theajax
option. With theajax
option you can useajax.data
to send your parameters to the server.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:Kevin
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.
Hi again, I tried this:
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.
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