How to return json object in javascript function?
Like many others already correctly described, the ajax request runs asynchronous by default. So you need to deal with it in a proper way. What you can do, is to return the jXHR
object which is also composed with jQuery promise maker. That could looke like
function getTags(level){ return $.getJSON("php/get-tags.php", { "parent": level });}
and then deal with it like
$(function(){ getTags('0').done(function(json) { // do something with json });});
You are trying to call an asynchronous function in a synchronous fashion.
When you call getTags, the it triggers a call to your PHP page, if javascript was blocking, then your page would hang until your server responded with the JSON. You need to re-think your logic and trigger a callback.
function getTags(level){ $.getJSON("php/get-tags.php", { "parent": level }, function(json) { //do something with the JSON here. });}
You cannot return from an AJAX call. It's asynchronous. You need to have all logic dealing with the returned data in the callback.