Ticket #6248 (closed bug: worksforme)
ajax calls success twice when server is unavailable
| Reported by: | kgustine | Owned by: | kgustine |
|---|---|---|---|
| Priority: | low | Milestone: | 1.4.3 |
| Component: | ajax | Version: | 1.4.2 |
| Keywords: | success firefox | Cc: | |
| Blocking: | Blocked by: |
Description
jQuery: 1.4.2; Firefox: 3.5.8
I have found that the following script calls success twice when the web server is turned off. This is proven by resetting testCount when the function is called, but incrementing it in success. If you run this in firefox and turn off the server, the alert box will first say 1, then 2.
This only happens the first time ajax is called after the server is turned off. It is not necessary to point ajax to a vaild page.
This is similar to bug #5968 and I'm sure is closely related.
This was discovered because I was clearing and setting Timeouts and Intervals inside the Success function. I was getting thousands of ghost intervals after a short period.
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script>
var testCount;
function callAjax()
{
testCount = 1;
$.ajax({
url: "http://localhost/blah.php",
success: function( msg ) {
alert( testCount++ );
}
});
}
callAjax();
setInterval( "callAjax()" , 5000 );
</script>
Change History
comment:2 Changed 3 years ago by snover
- Owner set to kgustine
- Priority set to undecided
- Status changed from new to pending
Is this still a problem in 1.4.3? Also, please be aware, passing a string to setInterval or setTimeout is an implicit call to eval: don’t do it.
comment:3 Changed 3 years ago by aboushley
So I don't think you actually need to turn off the web server, you just need to request something that doesn't exist. The first issue here is that fact that on success is being called when something is failing to load, this can be demonstrated in this jsfiddle http://jsfiddle.net/boushley/vrFhF/ however this behavior is only exhibited in jQuery 1.4.2 (as far as my testing in Chrome goes).
The issue you're having with the testCount being incremented twice I don't think is the same problem. I think that has more to do with the fact that you're using a closure to save the testCount and doing this on an interval. My guess is that with the timeout period callAjax is getting called twice setting testCount to 1 twice in a row, then both of those success function are being called after the second request has already been sent. Which will result in the 1st request showing one and the 2nd request showing two. A better test you could try out locally would be to just do a console .log when you call ajax... and then a console.log when you get success (or alerts) and just count them. How many success calls are there to how many callAjax calls.
If the success function really is being called twice for a single ajax request, that's something else we need to check into, so figuring out if that's the case would be great.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

It is not possible to use jsFiddle to simulate this because it requires actually stopping the web host service. In my case, Apache.