如何从$ .getJSON返回值 [英] How to return value from $.getJSON

查看:119
本文介绍了如何从$ .getJSON返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用$ .getJSON的方法上遇到问题.这非常简单,看起来像这样:

I'm having a problem with method using $.getJSON. It's very simple and it looks like that:

function lastID(query) {

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

    var type_id = json.data;

});

    return type_id // doesn't work
}

能否请您告诉我如何将type_id返回其他值,例如:

Could you please tell me how to return type_id to some other value for instance like this:

var returnedID = lastID(query); // this schould me equal to returned type_id from lastID method.

非常感谢您的答复

推荐答案

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

You will need to do something like the following, since you cannot return a value from an asynchronous request. This question has been asked many times. jquery - return value using ajax result on success and jQuery ajax return value

function processType(id){
    ....
}

function lastID(query){
    $.getJSON(url+query ,function(json){
        processType(json.data);
    });
}

这篇关于如何从$ .getJSON返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆