jqueryajaxxmlhttprequestjqxhr

jqXHR.statusText 'abort' vs 'canceled' when aborting the XHR


The jqXHR object used by jQuery.ajax has a property statusText, which can have two values when aborting the XHR: canceled and abort. When is the former used and when is the latter?


Solution

  • Here is the relevant source code of ajax.js:

            // Default abort message
            strAbort = "canceled",
    
            // Fake xhr
            jqXHR = {
    
                ...
    
                // Cancel the request
                abort: function( statusText ) {
                    var finalText = statusText || strAbort;
                    if ( transport ) {
                        transport.abort( finalText );
                    }
                    done( 0, finalText );
                    return this;
                }
            };
    
            ...
    
            // Apply prefilters
            inspectPrefiltersOrTransports( prefilters, s, options, jqXHR );
    
            // If request was aborted inside a prefilter, stop there
            if ( state === 2 ) {
                return jqXHR;
            }
    
            ...
    
            // aborting is no longer a cancellation
            strAbort = "abort";
    

    It reads that if you abort the XHR request in the prefilter, then the statusText is canceled, otherwise it is abort.