Good practices with $.get from jquery

Asked

Viewed 77 times

1

I believe it is not recommended to make one $.get within a $.get.

But then what would be the best practice for this code below:

var valorSalario = getSalarioAtual(location.state);

$.getJSON('https://geoip-db.com/json/geoip.php?jsonp=?').done (function(location) {

    if (salarioNacional(location.state)) {
        $("#salarioatual").html("Salário Mínimo Nacional <br/> R$" + this.valorSalario);
    } else {
        $("#salarioatual").html("Salário do seu estado (" + location.state + "): <br/> R$ 880.00");
    }
});

function getSalarioAtual(estado) {
    var estado = getSiglaEstado(estado);
    var salario = "";
    $.get('/salarios-estado/' + estado).done(function(response){
       salario = response.salario;
       return salario;
    });
}

What would be the best implementation for this solution? Already, I thank you.

  • You need to make both calls in parallel or one after the other?

  • one after the other, I already have that with the variable valueSalario filled to use inside the other request..

  • I see no problem in calling one GET inside another, because in this way, you guarantee that the result of the first call will be passed as parameter to the second call. That’s pretty common, I’ve used it that way.

  • Remembering that this way a GET is not being called inside another, but sequential.

  • Use location as variable name advise against as it is a global window. What is this.valorSalario, and confirm that both GET have as their starting point the location.state?

No answers

Browser other questions tagged

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