Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
How can I store the result of an AJAX query in a pre-declared variable?
$("#btn").click(function() {
    var data;
    getData();
    // use data...
}

function getData() {
    $.ajax({
        // Usual AJAX key-value pairs...
        success: function(response) {
            // Set data = response
        }
    });
}

How can I store the response to the AJAX request in the data variable declared in the handler function?

I want to use the value returned by the server inside the handler function.

Is there a way I can do this without disabling asynchronous retrieval.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Based on what you are asking, you could do the following, but I would suggest running your logic inside of the callback itself, as Javascript will not wait for the ajax response to complete before moving on within the function.

var getData = function( callback ) {

    $.ajax({
        success: function( response ) {

            callback( response );

        }
    });

};

$('#btn').on('click', function(evt) {

    var data;

    getData(function(response) {
        data = response;

        //you should use data here

    });

});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda