Bug Tracker

Ticket #5968 (closed bug: duplicate)

Opened 5 years ago

Last modified 4 years ago

AJAX failures don't call error but rather success

Reported by: bbirnbau Owned by:
Priority: major Milestone: 1.4.2
Component: ajax Version: 1.4.1
Keywords: ajax, error, success, callback Cc:
Blocking: Blocked by:

Description

I have observed that a failed ajax request does not properly call error upon failure. I have turned off my server and watch in firebug to see my successful requests turn to failures however the wrong events are called. Success does get called while error does not

I have confirmed that the code below works properly in jQuery 1.3.2 and is broken in 1.4.0 and 1.4.1

<script type="text/javascript"> <![CDATA[ setInterval(function() {if (ready_to_poll == true && init_tickets_to_load == 0 && polling_failures < 5) { d = new Date();ready_to_poll=false;; jQuery.ajax({complete:function(request){last_que_id=current_que_id;last_twitter_search_id=current_twitter_search_id;ready_to_poll=true; if (polling_failures == 5) { tb_show('error title','#TB_inline?height=300&width=600&inlineId=screen_pop_window&modal=true');$('#TB_ajaxContent').html('<div style="text-align:center;color:red;margin-top:80px;font-size:1.5em;padding:20px;">We are sorry but something went wrong. Please close your browser and try again...</div>');}}, data:'cqid='+current_que_id+'&lqid='+last_que_id+'&cqiids='+current_queue_items+ '&cqsoid='+current_queue_sort_order_id+'&cqpnum='+current_queue_page_num+'&cbt='+d.getTime()+'&ctsid='+current_twitter_search_id+'&ltsid='+last_twitter_search_id+'&ctsmaxid='+current_twitter_search_max_id + '&cvtsids='+current_viewed_tweet_search_ids + '&authenticity_token=' + encodeURIComponent(AUTH_TOKEN), dataType:'script', error:function(request){console.log('error poll');polling_failures++;}, success:function(request){console.log('success poll');polling_failures=0;}, type:'get', url:'/agent/poll'}); }}, 5 * 1000) ]]> </script>

Attachments

0001-Fix-jQuery.ajax-success-function-being-called-when-H.patch Download (1.8 KB) - added by nic_bellamy 4 years ago.
[PATCH] Fix jQuery.ajax() success function being called when HTTP connection refused.

Change History

comment:1 Changed 5 years ago by bbirnbau

I would like to add more information that may be helpful. I've learned that on 1.4.1 when there is a 500 that comes back from the server it does fire the error callback. Where it doesn't fire the error is when it can't connect to the server

comment:2 follow-up: ↓ 3 Changed 5 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to invalid

The error callback is called if the request times out, or if the request returns with an http error code (such as 500). The request hasn't returned (it can't reach the site apparently) so you should set a timeout.

comment:3 in reply to: ↑ 2 Changed 5 years ago by bbirnbau

  • Status changed from closed to reopened
  • Resolution invalid deleted

I'm not sure that we are saying the same thing here. In jQuery 1.3.2 when the server was not available error was called correctly. In jQuery 1.4.0 and 1.4.1 when an Ajax request can't reach the server it returns very quickly (no timeout is required) however the error callback is not fired and the success one is. This is not something that I can solve via a timeout and I honestly believe that when there is an error on an request (server not available) that jQuery should not ever return success.

comment:4 Changed 5 years ago by john

Do you have more information about what error codes the server is returning? Can you give us a dump of the XHR so that we can investigate further?

comment:5 Changed 5 years ago by bbirnbau

Please see the link to a screenshot of Firebug. The first 2 requests were fine then I turned off the server and you'll see that the next 3 have a status of TIMEOUT.

 http://grab.by/23Ka

Thanks so much for looking into this as we love using jQuery and have found 1.4.1 to be much faster than 1.3.2 and don't want to have to go back to it.

comment:6 Changed 5 years ago by bbirnbau

comment:7 Changed 5 years ago by Joseph Crotty

Today I set up a totally stripped down test web page with one link to call .ajax(). The POST url was my localhost and I had turned the webserver off. Firebug net panel XHR shows the request Status as Timeout just like bbirnbau screen shot above. In other words the webserver will never issue anything. FF will try to use it and issue an immediate Timeout as it is unavailable.

IE6-8 call the error: callback function with the textStatus value of 'error' and an XHR object. Chrome, FF, and Safari call the success: callback function with the textStatus value of 'success' and an empty XHR object.

comment:8 Changed 5 years ago by ris

I'm getting the same results as Joseph Crotty in jQuery 1.4.1 and 1.4.2 on Iceweasel (Firefox) 3.0.6

Everything causes a 'success', even calling abort() on the xhr.

Is this perhaps related to ticket #6060 ?

comment:9 Changed 5 years ago by peterl

I have the same problems, using firefox 3.6 and safari 4.0.

I've created the following test case:

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<button id="test">Click here to test</button>
<script>
$("#test").click(function() {
    var p = {};
    p.error = function() {
	alert("Error message");
    };
    p.success = function(rdata, status){
	alert("DATA: " + rdata + " -- " + status);
    }
    p.cache = false;
    p.type = "GET";
    p.url = "data.txt";
    $.ajax(p);
});

Create a file data.txt with a text in it. Start the webserver, click the button; it will give you the text with DATA: in front of it. Stop the webserver, so there is an connection refused, then click the button: it will still say DATA: which is proof the success callback is called, while it should not. Change the script to use jquery 1.3.2 instead of 1.4.2 to get the expected results: with jquery 1.3.2 the error callback is called when your http server is shut down....

comment:10 Changed 5 years ago by ris

Having done a bit more work with this, I would have to say this error is rather erratic. On some of my $.ajax requests this problem occurs, and the success function is called with a null data value. On other requests, everything works as it should.

I haven't yet managed to narrow down the problem cases.

comment:11 Changed 5 years ago by maqdev

There is many duplicate tickets with this problem, here is the one that contains a fix that should help I guess:

 http://dev.jquery.com/ticket/6060

comment:12 Changed 5 years ago by peterl

I've tried the patch from ticket:6060 but it did not work. I assume that this particular problem is not related to that ticket...

comment:13 Changed 4 years ago by nic_bellamy

This all appears to be related to changeset c3c94823f27ee1e935586d46fd86f055f3a3d97d that added handling for an Opera bug with 304 "Not Modified" responses.

Uploading a "works for me" patch that uses browser sniffing to only apply the 304 workaround when using Opera, so it doesn't break all the other browsers handling of refused connections.

See 0001-Fix-jQuery.ajax-success-function-being-called-when-H.patch attachment

Changed 4 years ago by nic_bellamy

[PATCH] Fix jQuery.ajax() success function being called when HTTP connection refused.

comment:14 Changed 4 years ago by dmethvin

  • Status changed from reopened to closed
  • Resolution set to duplicate

Merging all to dup #6060.

Note: See TracTickets for help on using tickets.