I have the following Javascript function, which should return an array of groups located in the database. It uses the $.getJSON() method to call get_groups.php , which is actually reading from the database.
function get_groups() { var groups = []; $.getJSON('get_groups.php', function(response) { for (var i in response) { groups.push(response[i]); } } return groups; }
Unfortunately, this function does not work as expected, because groups.push(response[i]); does not populate var groups = []; (since I understand that it populates some other groups array, possibly global).
Assuming I don't want to have a global variable groups , how would you solve this problem?
javascript scope jquery getjson
Misha moroshko
source share