Skip to main content

Bug Tracker

Side navigation

#6248 closed bug (worksforme)

Opened March 09, 2010 12:12AM UTC

Closed November 22, 2010 02:08AM UTC

Last modified March 15, 2012 10:53AM UTC

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:
Blocked by: Blocking:
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>
Attachments (0)
Change History (4)

Changed October 19, 2010 02:22AM UTC by kgustine comment:1

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

Changed October 19, 2010 05:32AM UTC by snover comment:2

owner: → kgustine
priority: → undecided
status: newpending

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.

Changed November 02, 2010 08:23PM UTC by aboushley comment:3

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.

Changed November 22, 2010 02:08AM UTC by jitter comment:4

priority: undecidedlow
resolution: → worksforme
status: pendingclosed

test case tested with Opera, Chrome and FF on jQuery 1.4.4. Works fine. Success callback not called but error callback triggered.