I put the ajax call in a variable, how can I call it again and pass some parameters to the data attribute of the ajax?.
var request = $.ajax({ URL: '/usage_analytics.php', type: 'get', data: {date_start: dt_date_start, date_end: dt_date_end}, dataType: 'json' }); request.done(function (r) { console.log(r); //my codes goes here });
now I have a date range picker, If i click apply button i just want to call the request variable to be able to trigger the ajax call again and pass some parameters.
$('#reportrange').on('apply.daterangepicker', function(ev, picker) { var picked_start = picker.startDate.format('YYYY-MM-DD'); var picked_end = picker.endDate.format('YYYY-MM-DD'); dt_date_start = picked_start; dt_date_end = picked_end; //call the request here and pass the dt_date_start and dt_date_end });
TIA
Advertisement
Answer
I don’t think you can do that using a variable. I suggest you to define a function that contains your ajax request then you can call this function into your code like this:
function getUsageAnalytics(dt_date_start, dt_date_end) { return $.ajax({ URL: '/usage_analytics.php', type: 'get', data: {date_start: dt_date_start, date_end: dt_date_end}, dataType: 'json', }); });
After declaring your function you can use it like this:
$('#reportrange').on('apply.daterangepicker', function(ev, picker) { var picked_start = picker.startDate.format('YYYY-MM-DD'); var picked_end = picker.endDate.format('YYYY-MM-DD'); getUsageAnalytics(picked_start, picked_end).done(function (r) { console.log(r); // your codes goes here }); });