So, in my view, it is the server who should perform the search and embed the data directly in the page that will be served (as a result of server processing).
In most cases (and I say this only because, in some cases, as when we do not have access to the server, it is really justifiable to perform this task via Javascript), there is no purpose in serving a blank page, and that remains "frozen" until an AJAX request returns the data from the server. I recommend that you do not use AJAX, and instead manage the page already with the data embedded in server side.
However, if you need to apply this model, just use the third parameter (callback
) of function getJSON()
(which is the function to be executed upon receipt of the reply) to proceed with the execution in an asynchronous manner:
$.getJSON(v_url, v_data, function(data){
prosseguirExecucao(data);
}
Where data
will be the literal object with your data.
If you want a synchronous run (which, as @bfavaretto pointed out, is that routine call category that prevents the program from processing anything else until the called routine ends), just run, before the previous script, the following excerpt:
$.ajaxSetup({
async: false
});
EDIT: the above section changes the way all AJAX requests, from then on, will be made by jQuery. Since getJSON()
is nothing more than a syntax abbreviation for a call AJAX()
common, it will suffer the effect of ajaxSetup()
also, but if your intention is to only request JSON synchronously, you need to restore the configuration to asynchronous mode again after the getJSON()
:
$.ajaxSetup({
async: true
});
puts the complete request to be easier to understand
– Otto