简体   繁体   中英

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 , so your callback function should implement your logic, something like that, ,因此您的回调函数应实现您的逻辑,例如,

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();

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