Javascript TypeError: variable undefined

1

I have the following problem in my javacript code, I have searched for all kinds of solutions and nothing.

Here I have my code ready and running trial version ajax.html there all

var arreglo;
    var area = new Array();
    var sigla = new Array();

function realizaProceso(){       

    $.ajax({
            data:  {},
            url:   'http://bioredsky.epizy.com/conexion.php',
            type:  'post',
            beforeSend: function () {
                    $("#status").html("Procesando, espere por favor...");
            },
            success:  function (response) {
                    arreglo = $.parseJSON(response);
            }
    });
}

    var con=0;
while(con<=arreglo.length-1){
     area[con]= arreglo[con];        
     alert("El area es: " + area[con]);
     con++;

     sigla[con]= arreglo[con];        
     alert("La sigla es: " +sigla[con]);
     con++;
     }

my previous test code that is in ajax.html I copy it in my project in get_turn.js where I have all the functions of javascript

    var arreglo;
    var area = new Array();
    var sigla = new Array();

function realizaProceso(){       

    $.ajax({
            data:  {},
            url:   'http://bioredsky.epizy.com/conexion.php',
            type:  'post',
            beforeSend: function () {
                    $("#status").html("Procesando, espere por favor...");
            },
            success:  function (response) {
                    arreglo = $.parseJSON(response);
            }
    });
}

    var con=0;
while(con<=arreglo.length-1){
     area[con]= arreglo[con];        
     alert("El area es: " + area[con]);
     con++;

     sigla[con]= arreglo[con];        
     alert("La sigla es: " +sigla[con]);
     con++;
     }

And this error appears when I inspect the code:

  

TypeError: fix is undefined

PostData:

1-the code that is in ajax.html works correctly.
2-The libraries also work correctly.
3-the url: http.Bioredsky ..... send an array to the function created in ajax with json_encode.
4- I have initialized Array as a New Arrays, I set it default value and nothing.
5-Url: bioredsky ..... is not the one I'm currently using, I brought them up with that example because I'm not on my pc.

Thank you.

    
asked by LeandroInc 15.12.2017 в 22:06
source

2 answers

2

The problem why this is happening to you is because you are trying to go through the fix without taking into account that this arrangement depends on the call to your server, which is done asynchronously. The while where you do this process should go right after you write arreglo = $.parseJSON(response) which is when really arranging information. Greetings.

    
answered by 15.12.2017 в 22:18
0

Ajax requests have a communication which means that when you do the request you do not know exactly the time in will give an answer.

That's why when you do the query and fill the arreglo it can last longer than the one in which javascript processes the following function and can not find data within arreglo .

  

Example with the code outside the ajax response. (Fails because it does not find data within variable arreglo )

    var arreglo;
    var area = new Array();
    var sigla = new Array();

function realizaProceso(){   
$.ajax({
  url:'https://jsonplaceholder.typicode.com/posts/1',
  method: 'GET'
}).then(function(data) {
  arreglo = data
});
}
realizaProceso()
    var con=0;
while(con<=arreglo.length-1){
     area[con]= arreglo[con];        
     alert("El area es: " + area[con]);
     con++;

     sigla[con]= arreglo[con];        
     alert("La sigla es: " +sigla[con]);
     con++;
     }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  

Example where we put the function within the success or in this case the then() that would be the same only that it is a promise .

     

Note: the example is only to note that it does not give an error because the data inside the array variable is found but it is only a test and does not include the same data to be processed.

var arreglo;
    var area = new Array();
    var sigla = new Array();

function realizaProceso(){   
$.ajax({
  url:'https://jsonplaceholder.typicode.com/posts/1',
  method: 'GET'
}).then(function(data) {
  arreglo = data
      var con=0;
while(con<=arreglo.length-1){
     area[con]= arreglo[con];        
     alert("El area es: " + area[con]);
     con++;

     sigla[con]= arreglo[con];        
     alert("La sigla es: " +sigla[con]);
     con++;
     }
});
}
realizaProceso()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 15.12.2017 в 23:46