So, in my opinion, it is the server that should perform the search and embed the data directly on the page that will be served (as a result of processing on the server).
In most cases (and I say this just because, in some cases, such 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, "frozen" until an AJAX request returns the data from the server. I recommend that you do not use AJAX, and instead generate the page already with the data embedded on the server side.
However, if you need to apply this model, just use the third parameter ( callback
) of the function getJSON()
(which is the function to be executed after receiving the response) to proceed with the execution asynchronously:
$.getJSON(v_url, v_data, function(data){
prosseguirExecucao(data);
}
Where data
will be the literal object with your data.
If you want a synchronous execution (which, as @bfavaretto pointed out, is the category of a routine call that prevents the program from processing anything else until the calling routine finishes ), , before the previous script, the following excerpt:
$.ajaxSetup({
async: false
});
EDIT : The above excerpt changes how all AJAX requests from then on will be made by jQuery. Since getJSON()
is nothing more than an abbreviation of syntax for a% common call, it will suffer the effect of AJAX()
as well, but if you just want to do the JSON request synchronously, you you need to restore the configuration to asynchronous mode again after ajaxSetup()
:
$.ajaxSetup({
async: true
});