Bug Tracker

Ticket #3442: jsonabort.patch

File jsonabort.patch, 3.4 KB (added by morgan, 5 years ago)

jsonp/script abort and timeout patch

  • jquery/src/ajax.js

     
    175175                s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s)); 
    176176 
    177177                var jsonp, jsre = /=\?(&|$)/g, status, data, 
    178                         type = s.type.toUpperCase(); 
     178                        type = s.type.toUpperCase(), 
     179                        requestDone = false; 
    179180 
    180181                // convert data if not already a string 
    181182                if ( s.data && s.processData && typeof s.data != "string" ) 
     
    206207 
    207208                        // Handle JSONP-style loading 
    208209                        window[ jsonp ] = function(tmp){ 
    209                                 data = tmp; 
    210                                 success(); 
    211                                 complete(); 
     210                                requestDone = true; 
     211 
     212                                if(!window[ jsonp ].aborted) 
     213                                { 
     214                                        data = tmp; 
     215                                        success(); 
     216                                        complete(); 
     217                                } 
    212218                                // Garbage collect 
    213219                                window[ jsonp ] = undefined; 
    214220                                try{ delete window[ jsonp ]; } catch(e){} 
    215221                                if ( head ) 
    216222                                        head.removeChild( script ); 
    217223                        }; 
     224 
     225                        window[ jsonp ].aborted = false; 
     226                        window[ jsonp ].abort = function() { this.aborted = true; } 
    218227                } 
    219228 
    220229                if ( s.dataType == "script" && s.cache == null ) 
     
    262271                                script.onload = script.onreadystatechange = function(){ 
    263272                                        if ( !done && (!this.readyState || 
    264273                                                        this.readyState == "loaded" || this.readyState == "complete") ) { 
    265                                                 done = true; 
    266                                                 success(); 
    267                                                 complete(); 
     274                                                 
     275                                                requestDone = done = true; 
     276                                                 
     277                                                if(!script.aborted) 
     278                                                { 
     279                                                        success(); 
     280                                                        complete(); 
     281                                                } 
     282 
    268283                                                head.removeChild( script ); 
    269284                                        } 
    270285                                }; 
     286 
     287                                script.aborted = false; 
     288                                script.abort = function() { this.aborted = true; }; 
    271289                        } 
    272290 
    273291                        head.appendChild(script); 
    274292 
    275293                        // We handle everything using the script element injection 
     294                        if(s.timeout > 0) 
     295                                setTimeout(function() 
     296                                { 
     297                                        if(requestDone) 
     298                                                return; 
     299 
     300                                        if(jsonp) 
     301                                                window[ jsonp ].abort(); 
     302                                        else 
     303                                                script.abort(); 
     304 
     305                                        if(s.error) 
     306                                                s.error("timeout"); 
     307                                }, s.timeout); 
     308 
     309                        // We handle everything using the script element injection 
     310                        if(jsonp) 
     311                                return window[ jsonp ]; 
     312                        if(script) 
     313                                return script; 
    276314                        return undefined; 
    277315                } 
    278316 
    279                 var requestDone = false; 
    280  
    281317                // Create the request object 
    282318                var xhr = s.xhr(); 
    283319