jQuery的getScript加入脚本函数永远不会失败?永远不会、脚本、函数、jQuery

2023-09-10 19:56:14 作者:╭學絵快ゞ樂

jQuery的getScript加入失败函数永远不会被调用。看到这个小提琴: http://jsfiddle.net/getsetbro/8xNMs/

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');
});

和完整的功能不会被调用: http://jsfiddle.net/getsetbro/ns6yQ/

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');
    }
})

哦,在IE浏览器,它实际上触发成功,并完成时,它会失败的。 = [

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

推荐答案

.fail 工作不跨域请求。

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

但它确定为同一个域。 http://jsfiddle.net/8xNMs/2/

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