Search code examples
jqueryajaxgetscript

jquery getScript function never fails?


The jQuery getScript fail function is never called. See this fiddle: http://jsfiddle.net/getsetbro/8xNMs/

$.getScript("http://api.jquery.com/scripts/jquery.NO-SUCH-FILE.js").done(function() {
    console.log('yep');
}).fail(function() {
    console.log('fail function does not fire fine');
});

And the complete function is never called: http://jsfiddle.net/getsetbro/ns6yQ/

$.ajax({
    url: url,
    type: 'get',
    crossDomain: true,
    dataType: 'script',
    async:false,
    cache:false,
    success: function(result) {
        console.log('SUCCESS');
    },
    error: function(result) {
        console.log('ERROR');
    },
    complete: function(result) {
        console.log('COMPLETE');
    }
})

Oh, and in IE it actually fires SUCCESS and COMPLETE when it should have failed. =[


Solution

  • .fail is not working for cross-domain request.

    // Bind script tag hack transport
    jQuery.ajaxTransport( "script", function(s) {
    
        // This transport only deals with cross domain requests
        if ( s.crossDomain ) {
        ...
        script = document.createElement( "script" );
    

    Script element fires no errors and such.

    But it's ok for same domain. http://jsfiddle.net/8xNMs/2/