I want to make an AJAX request "in the background" of my JavasSript function, but my script waits for the AJAX request to complete before it continues to execute my code.
$('div').hide();
$.get('/controller/action', { id: 'abc123' },
function(data){
//request completed
//now update the div with the new data
$('div').html(data);
}
);
$('div').slideDown('slow');
//by now hopefully the div has been updated
//and the user hasn't waited too long
The problem is that the slideDown animation waits to execute until the request has returned a response. How can I get the the animation to execute at the same time as the ajax request?
解决方案
You code should already be doing the Ajax request "in the background" (asynchronously). The jQuery.get() method is shorthand for:
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
And the jQuery.ajax() method is async: true by default.