tags:

views:

45

answers:

1

It looks like jQuery's .ajax function fires the 'success' function even when it gets no response from the server. I'd consider this an error. I detected this event by checking to see if the 'request.status===0', are there any other instances that I should check for where the 'success' function will be fired even if there is an error situation?

I've posted the code I'm using now below. How is this type of situation normally handled?

$.ajax({ 
    async : true, cache : false, dataType : 'json',
    type : 'POST',
    url : 'person/' + personKey,
    data : JSON.stringify({firstName:'Jilopi',}),

    success : function(data, textStatus, request){
        if( request.status === 0 ){
            alert('Error: problem saving person - no response');
        }else{
            alert('Success: saved person');
        }
    },
    error : function(request, textStatus, errorThrown){
        alert('Error: problem saving person');
    },
});
+8  A: 

You've identified the only "false success" issue I know of. To elaborate on it a bit: this was a work-around in place strictly for Opera (which returns what's actually a 304 as a 0), you can see the comment in 1.4.2 (and prior versions) here: http://github.com/jquery/jquery/blob/1.4.2/src/ajax.js#L551

However, it has been removed in jQuery 1.4.3 because of your exact issue, you can see the change that did it here.

Short answer: upgrade to jQuery 1.4.3+ to resolve your issue and you can do away with your status === 0 check.

Nick Craver
Awesome, thanks a lot
DutrowLLC