jquery getScript function never fails?

岁酱吖の 提交于 2019-12-10 19:56:56

问题


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. =[


回答1:


.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/




回答2:


cross-domain .fail & .always work with jQuery 2.0

$.getScript("http://api.jquery.com/scripts/NO-SUCH-FILE.js")
  .done(function() {
    console.log("done fired");
  }).fail(function() {
    console.log("fail fired");
  }).always(function() {
    console.log("always fired");
});

http://jsfiddle.net/c2gyy/1/



来源:https://stackoverflow.com/questions/13476497/jquery-getscript-function-never-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!