2
It is possible to associate a value received by $.ajax
a global variable, because from these values, I need to add them and show them in another table. I tried but always interprets with local variable, losing its value at the end of the function.
Follows code JS:
function buildTableNI(){
$('#tb_ni').empty();
$.ajax({
type:'GET',
crossDomain:true,
url:'http://www.minhaurl.com.br/api/meuphp.php?callbackpni=?',
dataType:'jsonp',
data: {currency: $('#cur').val()},
beforeSend: function(){
$('#loading').css("display","block");
$('table[name=tb_ni]').css("opacity","0.01");
}
}).done(function(data){
console.log(data);
$('#loading').css("display","none");
$('table[name=tb_ni]').css("opacity","1");
$('#tb_ni').append('<tr> <td class="column_st">'+'Active'+
'</td><td class="column_qtd">'+data.ni_qtdA+
'</td><td id="" class="a">'+data.ni_active+
'</td><td>'+data.ni_p_active+'</td></tr>');
// quero pegar esses valores (data.ni_active,
//data.ni_p_active,etc e colocar seu valor em uma varivel global.)
a = $('#ac3').append(parseInt(data.ni_qtdA));
$('#tb_ni').append('<tr> <td class="column_st">'+'Inactive'+
'</td><td class="column_qtd">'+data.ni_qtdI+
'</td><td id="a3" class="i">'+data.ni_inactive+
'</td><td>'+data.ni_p_inactive+'</td></tr>');
$('#tb_ni').append('<tr> <td class="column_st">'+'Won'+
'</td><td class="column_qtd">'+data.ni_qtdW+
'</td><td class="w">'+data.ni_won+
'</td><td>'+data.ni_p_won+'</td></tr>');
$('#tb_ni').append('<tr> <td class="column_st">'+'Budget'+
'</td><td class="column_qtd">'+data.ni_qtdB+
'</td><td class="b">'+data.ni_budget+
'</td><td>'+data.ni_p_budget+'</td></tr>');
$('#tb_ni').append('<tr> <td class="column_st">'+'Coming'+
'</td><td class="column_qtd">'+data.ni_qtdC+
'</td><td class="b">'+data.ni_coming+
'</td><td>'+data.ni_p_coming+'</td></tr>');
$('#tb_ni').append('<tr> <td class="column_st">'+'In Process'+
'</td><td class="column_qtd">'+data.ni_qtdP+
'</td><td class="p">'+data.ni_process+
'</td><td>'+data.ni_p_process+'</td></tr>');
$('#tb_ni').append('<tr> <td class="column_st">'+'N/I'+
'</td><td class="column_qtd">'+data.ni_qtdNI+
'</td><td class="ni">'+data.ni_ni+
'</td><td>'+data.ni_p_ni+'</td></tr>');
$('#tb_ni').append('<tr class="head_table"> <td>'+'Total'+
'</td><td class="column_qtd">'+data.ni_qtd_total+
'</td><td class="total">'+data.ni_total+
'</td><td>'+data.ni_p_total+'</td></tr>');
$('#tb_ni').append('<tr> <td class="column_st">'+'Replaced'+
'</td><td class="column_qtd">'+data.ni_qtdR+
'</td><td class="r">'+data.ni_replaced+
'</td><td>'+' - '+'</td></tr>');
})
.fail(function(data, textStatus, errorThrown){
alert("Erro na operação.");
console.log(data);
console.log(textStatus);
console.log(errorThrown);
});
return false;
}
In short, I need to take these figures(data
) received at .done
and assign them to global variables, because I have more functions that need these values to be summed.
EDIT:
I tried to do using window.variavel_global
in that passage:
}).done(function(data){
window.vg = data.ni_active;
console.log("variavel global:"+(vg));
It even shows in the console, but when I check in the console the value of this variable outside of this function, like this:
console.log( window.vg);
It doesn’t work and returns Undefined on the console.
Hi, I tried to use with window and it still didn’t work. The variable comes empty, I checked the console.
– Léo
It was supposed to work. I added an example in the code.
– StillBuggin
It didn’t work anyway :( I edited my question.
– Léo