如何从$ .getJSON返回值

0 jquery return getjson

我使用$ .getJSON的方法有问题.它非常简单,看起来像这样:

function lastID(query) {

  $.getJSON(url+query ,function(json){

    var type_id = json.data;

});

    return type_id // doesn't work
}
Run Code Online (Sandbox Code Playgroud)

你能告诉我如何将type_id返回到其他值,例如:

var returnedID = lastID(query); // this schould me equal to returned type_id from lastID method.
Run Code Online (Sandbox Code Playgroud)

非常感谢你的回复

Cal*_*lum 6

您将需要执行以下操作,因为您无法从异步请求返回值.这个问题已被多次询问.jquery - 使用ajax结果返回值成功和jQuery ajax返回值

function processType(id){
    ....
}

function lastID(query){
    $.getJSON(url+query ,function(json){
        processType(json.data);
    });
}
Run Code Online (Sandbox Code Playgroud)