获取 jQuery get().done() 函数的返回值

get return value of jQuery get().done() function

本文关键字:返回值 函数 done jQuery 获取 get      更新时间:2023-09-26

我有代码:

function images(){
    $.get("page").done(function(data) {
        manipulation to get required data
    });
}

我尝试使用全局变量、返回值和函数外部的函数从 get 函数内部获取变量。有谁知道如果我调用images(),我将如何从 get 函数内部获取变量并将其作为值返回?

你不能这样做,因为$.get()异步执行。解决方案是在图像中使用回调来处理 $.get() 返回的值

function images(callback){
    $.get("page").done(function(data) {
        manipulation to get required data
        var d = ? //manipulated data that was supposed to be returned
        callback(d);
    });
}

然后

//calls images
images(function(data){
    //this method will get called when the $.get() is completed, and data will be the value passed to callback(?) in the done function
})

更多:阅读此内容