-2
I have the following requisition ajax
where it returns the value in success
:
function novasMensagens(Alerta) {
var retorno = Alerta > 0 ? Alerta : "";
$.ajax({
type: 'GET',
url : './fetchbusca',
success : function(data) {
console.log(data);
var Alerta = data;
console.log(Alerta);
}
});
}
Return: {"0":"1","Alerta":"1"}
Now the value that returns is {"Alerta":"1"}
I have to assign it to the Alert that is within the return variable:
var retorno = Alerta > 0 ? Alerta : "";
@Sam What I intend to take is this value returned within the
sucess
and use it in another role, as I can?– Bruno
@Sam understood, but I did it and I get the mistake
Alerta is not defined
. Functionfunction novasMensagens(Alerta){ $.ajax({ type: 'GET', url : './fetchbusca', success : function(data) { var Alerta = data; var retorno = Alerta > 0 ? Alerta : ""; return retorno; } }); } document.getElementById("msgNumero").innerHTML = novasMensagens(Alerta );
– Bruno
When you perform
document.getElementById("msgNumero").innerHTML = novasMensagens(Alerta );
Ajax has not yet been processed. Ajax is asynchronous.– Sam
@Sam means I have to put Javascript inside the
header
?– Bruno
No. Everything that comes from Ajax you must do inside the Success function. That’s where the values are returned.
– Sam