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?
– Douglas Garrido
one after the other, I already have that with the variable valueSalario filled to use inside the other request..
– Jose Vieira Neto
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.– Douglas Garrido
Remembering that this way a
GET
is not being called inside another, but sequential.– MarceloBoni
Use
location
as variable name advise against as it is a globalwindow
. What isthis.valorSalario
, and confirm that bothGET
have as their starting point thelocation.state
?– Sergio