I am not able to print the values added to my Array in the Console. The Console response is empty as if it had not added the values to the Array. Below is the code and XML Document.
Code Block:
$(document).ready(function(){
var Gostei = []; // Array que vai armazenar as notas boas
$.ajax({
url:'NotasPorMes.xml',
dataType: 'xml',
success: function(xml){
$(xml).find('list').each(function() {
$(this).find('NotasPorMes').each(function(){
// Adicionando os valores de Gostei para o Array
Gostei.push([$(this).find('Gostei').text()]);
});
});
},
// Se nao consegui ler o arquivo xml, exibo mensagem de erro no console
error: function () {
console.log("Ocorreu um erro inesperado durante o processamento.");
}
});
console.log(Gostei); //Imprime o valor do Array
});
XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<list>
<NotasPorMes>
<Mes>1</Mes>
<Gostei>9</Gostei>
<NaoGostei>6</NaoGostei>
<Total>7</Total>
</NotasPorMes>
<NotasPorMes>
<Mes>8</Mes>
<Gostei>6</Gostei>
<NaoGostei>9</NaoGostei>
<Total>7</Total>
</NotasPorMes>
<NotasPorMes>
<Mes>6</Mes>
<Gostei>4</Gostei>
<NaoGostei>9</NaoGostei>
<Total>8</Total>
</NotasPorMes>
</list>
The value is only printed if I put the console.log(Gostei);
inside the bloco $(this).find('NotasPorMes').each( function(){...});
, Since the purpose of the code is to go through the xml add the values to the array and then returns that array with its values for due treatment outside the block $.ajax({...});
.
I hope to be very clear, I count on your help, Thank you.