It is possible to associate a value received with $.ajax
with a global variable, because from these values, I need to add them and show them in another table. I have tried it however it always interprets with local variable, losing its value at the end of the function.
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 get these values ( data
) received in .done
and assign them to global variables, as I have more functions that need these values to be summed.
EDIT:
I tried to use window.variavel_global
in this section:
}).done(function(data){
window.vg = data.ni_active;
console.log("variavel global:"+(vg));
Even shows in the console, but when I check the console of this variable outside of this function, like this:
console.log( window.vg);
It does not work and returns undefined on the console.