Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
How to call variabled ajax

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
  });
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!