Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda