0

I have an ajax call when my page loads, the result is a integer number that I need to save in a variable to use later with other jquery functions.

This is my code right now

$(document).ready(function() {
    var ajaxResult = new Array();
    $.ajax({
        url: '<?php echo base_url(); ?>index.php/Po_general/obtenerIdConsecutivo',
        method: 'GET',
        success: function (returned) {
            var returned = JSON.parse(returned);
            var idPog = returned.idPog;
            ajaxResult.push(idPog);
        }
    });
    //This shows me the array in the console
    console.log(ajaxResult);
    //This shows undefined
    console.log(ajaxResult[0]);
    /*
     * Additional jquery functions
     */
});

console.log(ajaxResult) shows me the array in the console

[]
   0: 2
   length: 1

But when I want to access ajaxResult[0] it shows as undefined.

raptorandy
  • 225
  • 5
  • 21

1 Answers1

-1

Can you try with this -

$(document).ready(function() {
var ajaxResult = [];
$.ajax({
    url: '<?php echo base_url(); ?>index.php/Po_general/obtenerIdConsecutivo',
    method: 'GET',
    success: function (returned) {
        var returned = JSON.parse(returned);
        var idPog = returned.idPog;
        ajaxResult.push(idPog);
    }
});
//This shows me the array in the console
console.log(ajaxResult);
//This shows undefined
console.log(ajaxResult[0]);
/*
 * Additional jquery functions
 */

});

Ganesh
  • 52
  • 6