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

115
Visualizações
JS AJAX request return response
function getRequest(url)
{
    return $.ajax({
        url: url,
        type: "GET",
    })
}

function ABC()
{
    let url = "simple/url";
    let response = GET.getRequest(url);
    return response;
}

let global_var = "";

$.when(ABC()).then(response => 
{
    global_var = response;
    console.log(global_var); // -> it will show response
});


$(document).ready(function()
{    
    console.log(global_var); // -> empty string
});

Is there a way to pass response to global_var ? Code in document.ready function is executing before ajax calls, so thats why global_var is empty string but is there any other way to deal with it ? I can of course move whole document. ready function to $_when() but I want to avoid it becasue code will be less readable. How to wait for global_var assigment by response ?

about 4 years ago · Juan Pablo Isaza
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