I have this code below, which is supposed to return the result of the call. I need to do this synchronously so that I know everything is good, however it doesn\'t seem to work.
$.getJSON is a shorthand for $.ajax
.
This is a shorthand Ajax function, which is equivalent to:
$.ajax({ dataType: "json", url: url, data: data, success: success });
You'll notice that there is no option for passing through an async option. The parameter you are attempting to add async: false
to is actually the data that will be sent to the url
with the ajax request.
Try doing this instead:
$.ajax({
dataType: "json",
url: url,
async: false,
data: data,
success: success
});
Also, your statement
I need to do this synchronously so that I know everything is good
is incorrect. You can 'know everything is good' from the asynchronous callback. Your sample code would do what you are trying to do above exactly if you wrote it like this:
function getJSON(url){
var result;
$.getJSON(url, function(data) {
alert(data);
// do other stuff with data, call other methods etc. etc.
});
}
You could even define your callback function separately and pass it to $.getJSON
, like so:
function jsonCallback(data) {
alert(data);
// do other stuff with data, call other methods etc. etc.
}
function getJSON(url){
var result;
$.getJSON(url, jsonCallback);
}