首页 文章

如何通过ajax调用从另一个目录同步加载脚本?

提问于
浏览
13

我经常需要通过ajax加载其他javascript文件,所以在开始时我使用jQuery提供的标准函数来加载脚本:

$.getScript('script_name.js',callback_function());

但是这没有't work out, since $.getScript is asynchronous (the jQuery API for $.ajax says ' async'默认设置为true;主题在API的注释中讨论,$ .getScript:http://api.jquery.com/jQuery.getScript/) . 所以我写了这个函数,由上面链接的API页面的评论中的某人提供:

load:function(script,callback){

    jQuery.ajax({

        async:false,

        type:'GET',

        url:script,

        data:null,

        success:callback,

        dataType:'script'

    });

},

这似乎运作良好,所以我继续,但我最近注意到,这只适用于同一目录中的脚本,例如 . 调用myObj.load('test.js')效果很好,但调用myObj.load('test / test.js')根本不起作用 .

感觉我错过了一些明显的东西,但我没有设法找到问题 . 任何的想法?

2 回答

  • 13

    Update: See the comment stream below, it's nothing to do with jQuery, it's a file permissions problem on the server.


    Original answer

    你从浏览器中得到任何错误吗?例如,在Chrome或Safari中,如果您打开开发工具并查看控制台选项卡,它是否显示错误?或者在Firefox中,安装Firebug并检查Firebug的控制台 . 或者在IE中,使用免费版的VS.Net ......有些事情应该向你抱怨 .

    您还可以通过提供 error 函数而不是假设成功来从代码本身获取更多信息:

    jQuery.ajax({
        async:false,
        type:'GET',
        url:script,
        data:null,
        success:callback,
        dataType:'script',
        error: function(xhr, textStatus, errorThrown) {
            // Look at the `textStatus` and/or `errorThrown` properties.
        }
    });
    

    Update :你说过你看到 textStatus = 'error'和 errorThrown =未定义 . 很奇怪 . same 脚本是否可以移动,因为它不在子路径上?我想知道子路径是否是红色鲱鱼,真正的问题是脚本中的语法错误 .


    Off-topic :它真的必须同步吗?你可以't just poll for a symbol to appear? It'只是同步ajax请求 really 垃圾用户体验 . 在许多浏览器中,不仅是您自己的页面,而且 all 页面在请求期间锁定 .

    这就是我的意思:轮询:假设我想从JavaScript异步加载jQuery:

    function loadScript(url, symbol, callback) {
        var script, expire;
    
        // Already there?
        if (window[symbol]) {
            setTimeout(function() {
                callback('already loaded');
            }, 0);
        }
    
        // Determine when to give up
        expire = new Date().getTime() + 20000; // 20 seconds
    
        // Load the script
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = url;
        document.body.appendChild(script);
    
        // Start looking for the symbol to appear, yielding as
        // briefly as the browser will let us.
        setTimeout(lookForSymbol, 0);
    
        // Our symbol-checking function
        function lookForSymbol() {
            if (window[symbol]) {
                // There's the symbol, we're done
                callback('success');
            }
            else if (new Date().getTime() > expire) {
                // Timed out, tell the callback
                callback('timeout');
            }
            else {
                // Schedule the next check
                setTimeout(lookForSymbol, 100);
            }
        }
    }
    

    用法:

    // Load jQuery:
    loadScript("path/to/jquery.min.js", "jQuery", function(result) {
        // Look at 'result'
    });
    
  • 17

    您可以使用ajaxSetup将ajax调用设置为默认同步 . 这是它的样子:

    $.ajaxSetup({async:false});
    $.getScript('script_name.js', callback_function);
    

    如果您需要再次进行异步调用,只需启用它:

    $.ajaxSetup({async:true});
    

相关问题