datatables ajax data function not sending data through but ajax data json does
datatables ajax data function not sending data through but ajax data json does

I build my datatables with data from obtained from ajax. When the html page loads, it calls a script that runs an ajax request to a python flask route which then uses SQLAlchemy to collect data from the database table.
I pass (stat_date and end_date) data to the ajax call so the data collected can be between the required dates.
Problem is, I cant receive request data on my flask route if the data is sent as a ajax data function but I do if I just send the data as json. Thing is, I have to use ajax data function so I can use ajax.reload() at a later stage to update my table based on the selected date range.
var s_date = moment().format();
var e_date = moment().format();
$.ajax({
type: "POST",
url: "../blog_data/",
contentType: "application/json",
data: {
"start_date" : s_date,
"end_date" : e_date
},
cache: false,
async: false,
success: function(data){
var my_columns = get_columns(data);
var dt_instance = $('#dt_usr_blog').DataTable({
"select":{
style: 'single'
},
"data": data,
"columns": my_columns,
"columnDefs": [
{
"render": function ( data, type, row ) {
return new moment(data).format("YYYY-MM-DD HH:mm:ss");
},
"targets": datetime_indices
},
{
"render": function ( data, type, row ) {
return users[data]['usr_a05_email'];
},
"targets": users_indices
}
],
"dom": '<"top"<"pml"><"pmr"B>>rt<"bottom"lip>',
"buttons": [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"lengthMenu": [ 8, 16, 24, 50, 100 ],
"initComplete": function(settings, json) {
.......
}
}
}
I've tried many options and read through this good url (tps://stackoverflow.com/questions/10434599/how-to-get-data-received-in-flask-request ) and many more but I still cannot locate the problem.
In ajax, if I send data to:the server as a json object (see below), it works
data: {
"start_date" : s_date,
"end_date" : e_date
},
but if I send data via a ajax data function (see below) as recommended in datatables documentation ajax.reload(), (https://datatables.net/reference/api/ajax.reload())
data: function(d){
d.start_date = s_date;
d.end_date = e_date;
},
the data wont show up on the Flask route request object.
What am I missing? Please help.
This question has an accepted answers - jump to answer
Answers
You are using the jQuery Ajax not the
ajax
Datatables option. According to the jQuery Ajax docs thedata
option supports the following types: PlainObject or String or Array. It does not support a function.Since you are not using the
ajax
option then, I believe,ajax.reload()
won't work. You could try usingajax.url()
to set the URL but not sure there is a way to set thedata
option.Instead of using
ajax.reload()
you probably will need to duplicate the above code in a function and call the function. In the success function useclear()
to clear the Datatable data then userows.add()
to add the retrieved data.Kevin
...oops... that explains it .... how did I miss that? tnx kthorngren. i'm gonna have to redesign so that I use the DataTables ajax option.