简体   繁体   中英

how to get data from another ajax based function?

function get_images(off){
    $.post('images_pro.php', {fn: 'get_images', args: [off, lm, dim, 'e']}, function(data){
        console.log(data);  // this works
        return data;
    });
}

$('.mmdim').on('click', function(){
    var data = get_images(0);
    console.log(data);  // undefined
});

How to get data from the first to the second function?

In your case, there are 2 options to do that:

Method 1 (using done function):

function get_images(off){
    return $.post('images_pro.php', {fn: 'get_images', args: [off, lm, dim, 'e']});
}

$('.mmdim').on('click', function(){
    get_images(0).done(function (data) {
        console.log(data);
    });
});

Method 2 (using async function ):

function get_images(off){
    return $.post('images_pro.php', {fn: 'get_images', args: [off, lm, dim, 'e']});
}

$('.mmdim').on('click', async function(){
    var data = await get_images(0);

    console.log(data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM