Assign received value via ajax to a variable

Asked

Viewed 1,809 times

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" of the ajax request?

Got it this way:

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

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

How can I fix this?

  • What do you mean by making the request? I’m using $(Document). ajaxStop(Function() {...}) to manage the values I receive only at the end of all ajax requests

3 answers

2

You can take the value obtained via ajax this way:

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

    }
});
  • How can I adapt this to the function I have? I just want to return the 'state' as it is in my function"

1

I’m not sure I understand what you want to do. Do you make a query that returns the articles right? And depending on the return of articles 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);
}
  • $(Document). ajaxStop({ Success: Function (data) { console.log("SUCCESS"); } }); This gives me error : Uncaught Typeerror: ((n.event.special[g.origType] || (Intermediate value). Handle || g.Handler). apply is not a Function ------ I’m using ajaxStop I don’t know if it has any influence.

  • Could you show me your code, so I can understand it better? You already have the values of "articlesDebitoManual" or search it via Ajax?

  • I edited the question and put before the ajax requests that are within a each cycle, hence have used the $(Document). ajaxStop to store the values I want at the end of the requests to be made, because it works asynchronously.

  • I edited the answer above. As I understood your problem is just where to throw the return. If you create a state variable outside your function and assign that it is equal to the return of your function, you will already have the value elsewhere. Take a look at the code of this answer and see if it helps you.

  • You understood perfectly what I wanted. I tested your code but the console.log(status) returns Undefined. That’s the problem. Thanks in advance for your help!

  • The state variable outside the ajax request, and perhaps because it is an asynchronous request, does not receive the true or false of the conditions.

Show 1 more comment

-3

A variable declared with var in the function is private, with this becomes pubic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.