jquery getScript 函数永远不会失败?

标签 jquery ajax getscript

永远不会调用 jQuery getScript 失败函数。看看这个 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/

$.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 中,它实际上在应该失败的时候触发了 SUCCESS 和 COMPLETE。 =[

最佳答案

.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" );

脚本元素不会引发错误等。

但对于同一个域来说是可以的。 http://jsfiddle.net/8xNMs/2/

关于jquery getScript 函数永远不会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13476497/

相关文章:

php jquery jcrop 和 imagejpeg

javascript - 为什么第一个网络调用比后续调用花费更多时间?

html - 终极网站测试字符串

javascript - 让 $.getScript ("") 不在请求末尾添加数字

jquery - $(窗口).height() : Firefox "loading-bar" in footer blocks 17px

jquery - 过滤时使用 jquery 显示 xml 的所有元素

javascript - 重新填充选择框的有效方法?

java - 在 ajax 调用中通过 id 获取 session

jquery - 我可以在没有回调的情况下使用 jquery getScript() 吗?

jquery - getscript 可以在 jquery noconflict 中运行导入的脚本吗?