2
I am unable 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 follows the code and the 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);
within the bloco $(this).find('NotasPorMes').each( function(){...});
, Since the purpose of the code is to traverse the xml add the values to the array and then return this array with its values for due processing outside the block $.ajax({...});
.
I hope I’ve been clear, I count on your help, thank you.
change the array creation, take var. ex: "I like=[];"
– Fernando Rangel