0

I want to recover the contents of my json but i can't. and the console "say Uncaught TypeError: Cannot read property 'longitude' of undefined" I'm very new with this method. Thank you if you can help me.

var recuptable = [];

     $(document).ready(function(){
     $.ajax({
         url: 'http://******',
         type: 'get',
         dataType: 'JSON',
         success: function(response){         

           recuptable.push(response);
         }

     });

 });

 console.log(recuptable[1].longitude);

Tytanpolo
  • 5
  • 1

1 Answers1

0

Your code is async, when you console log your variable it is still empty. Instead, move the console.log inside the success function.

var recuptable = [];

     $(document).ready(function(){
     $.ajax({
         url: 'http://******',
         type: 'get',
         dataType: 'JSON',
         success: function(response){         
           console.log(response.longitude);
           recuptable.push(response);
         }

     });

 });

Also, I suggest you read a little bit about callbacks and/or about promises so that you are able to work with async code more easily.

cefeboru
  • 332
  • 3
  • 4
  • 12