I can not print the values of my Array.

2

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.

    
asked by anonymous 20.11.2015 / 15:30

2 answers

1

What is happening is that your AJAX request is assíncrona , which means that when you do the request the javascript will not "wait" for the request to be completed to execute the next block of code.

You can make your request síncrona by changing the property assync to false .

If you'd like to understand it better, follow documentation in English.

$.ajax({
    url:'NotasPorMes.xml',
    dataType: 'xml',
    async: false  
});
    
20.11.2015 / 15:59
1

This happens because the $.ajax method is asynchronous by default, that is, it will run in parallel, and its console.log(Gostei); will run before it finishes.

As an alternative you say to the $ .ajax method you do not want it to be asynchronous by passing async: false to the options:

$(document).ready(function(){
          var Gostei = []; // Array que vai armazenar as notas boas
         $.ajax({
                  url:'NotasPorMes.xml',
                  dataType: 'xml',
                  async: false,
                  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()]);
                        }); 
                        console.log(Gostei); //Imprime o valor do Array    
                   });   
                },
                   // Se nao consegui ler o arquivo xml, exibo mensagem de erro no console
                   error: function () {
                      console.log("Ocorreu um erro inesperado durante o processamento.");
                    }
       });

    }); 

Or put the log into the success function:

$(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()]);
                        }); 
                        console.log(Gostei); //Imprime o valor do Array    
                   });   
                },
                   // Se nao consegui ler o arquivo xml, exibo mensagem de erro no console
                   error: function () {
                      console.log("Ocorreu um erro inesperado durante o processamento.");
                    }
       });

    }); 
    
20.11.2015 / 16:08