I use jQuery to do some asynchronous background data fetching. I stumbled upon the problem, that more than one parallel request to the same URL does not finish, until the first request handler is finished.
I created a simple example to demonstrate the problem:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<button id="js">Click me!</button>
<div>Request 1: <span id="resp1">-</span></div>
<div>Request 2: <span id="resp2">-</span></div>
<script type="text/javascript">
$('#js').click(function() {
$('#resp1, #resp2').html('...');
$.ajax({
url : 'http://stackoverflow.com',
async : true,
complete: function(request, textStatus) {
$('#resp1').html('complete');
alert('The second request is not completed, until you close this alert!');
}
});
$.ajax({
url : 'http://stackoverflow.com',
async : true,
complete: function(request, textStatus) {
$('#resp2').html('complete');
}
});
});
</script>
</body>
</html>
After the button is clicked, two parallel AJAX-Requests are made. The first one is completed normally, but the second not until the alert of the first one is closed. The problem remains the same, if you move the second ajax Request into the complete-Event handler of the first one.
Is this a bug or a feature? Is there a workaround?
Edit:
OK, I understand that alert() causes the problem. However I can't explain two other things.
When I move the second ajax-call into the complete handler of the first (before the alert()) and change the first ajax-call to be async:false, then it works. Shouldn't the alert() be causing the same problem??
The alert must be blocking more than the Browsers JS-Thread, because if I open the AJAX-Request in another tab, it still fails to load until I close the alert-Box in the first tab. JS not involved here. Strange. Is this perhaps another problem?
alert()- that's what is blocking, not the request.alertcompletely halts the browser's JS thread. That's the issue. Always useconsole.logfor debugging.