当我的页面加载时,我有一个ajax调用,结果是一个整数,我需要保存在一个变量中,以便以后与其他jquery函数一起使用。
这是我现在的代码
$(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)向我展示了控制台中的数组
[]
0: 2
length: 1
但是当我想访问ajaxResult [0]时,它显示为未定义。
答案 0 :(得分:-1)
你可以尝试这个 -
$(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
*/
});