简体   繁体   English

jQuery getJSON从函数返回对象数组?

[英]JQuery getJSON return object array from function?

Is it possible to return the value of an async call like the example below without making it synchronous? 是否可以像下面的示例一样返回异步调用的值而不使它同步?

var allUsers;

function getUsers() {
    jQuery.getJSON('http://127.0.0.1:8000/members', function(json) {
        return json;
    });
}

allUsers = getUsers();

A callback function can't return anything because the point of the callback functions is that you don't wait for the return , so your callback function should implement your logic, something like that, 回调函数无法返回任何内容,因为回调函数的目的在于您不必等待return ,因此您的回调函数应实现您的逻辑,例如,

function getUsers() {
    jQuery.getJSON('http://127.0.0.1:8000/members', function(allUsers) {
        if(allUsers) {
            // you have allUsers here
            // you can do whatever your logic requires here
        }
    });
}

getUsers();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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