Assigning value received via ajax to a variable

1

I have the following function:

var idArtigo = $(".codigo", this).text();
            myJSRoutes.controllers.ArtigoDocumentoController.getArtigoByDebitoTipo(parseInt(idArtigo)).ajax({
                success: function (data) {
                    if(data == true){
                        console.log("O ARTIGO_ID"+idArtigo+" tem debito manual");
                       artigosDebitoManual.push(parseInt(idArtigo));
                    }
                }
            });

function testeStateDebito(retorno){
            var state;
            $(document).ajaxStop(function() {
                if(artigosDebitoManual.length > 0){
                    state = true;
                }else{
                    state = false;
                }
                retorno(state);
            });
        }

How can I assign this "return" to a variable to use "outside" the ajax request?

I have this way:

testeStateDebito(function(cont) {
            console.log(cont);
            return cont;
        });

The console.log returns true or false, but I would like to have this state in a variable, like: var estado = 'valor retornado';

How can I resolve this?

    
asked by anonymous 21.05.2015 / 16:54

3 answers

2

You can get the value obtained via ajax like this:

$.ajax({ url: 'exemplo/consulta',
    type: 'GET',
    success: function (dados) {
        var resultado = dados;
    },
    error: function () {

    }
});
    
21.05.2015 / 17:13
1

I do not know if I understood correctly what you want to do. Do you make an inquiry that returns you the right articles? And depending on the return of articles do you want to return "true" or "false"?!

var estado;

var artigosDebitoManual = ['1', '2', '3'];


function testeStateDebito(retorno) {
    var state;

    $(document).ajaxStop(function () {
        if (artigosDebitoManual.length > 0) {
            state = true;
        } else {
            state = false;
        }
    });

    return state;
}

function teste() {
    estado = testeStateDebito();
    console.log(estado);
}
    
21.05.2015 / 17:39
-3

A variable declared with var in the function is being deprived, since this is pubic.

    
03.10.2015 / 19:19