【问题标题】:Add custom referer when fetching script via $.getscript()通过 $.getscript() 获取脚本时添加自定义引用
【发布时间】:2015-08-03 16:33:50
【问题描述】:

我用

获取脚本
$.getScript("http://www.example.org/file.js");

但是,该网站的引荐来源网址为空。如何使用referer(可以是antyhing)而不是空的referer?注意:需要使用getscript,我不能使用script src=""。

【问题讨论】:

  • 你到底想做什么?通过 AJAX 向目标站点发送标头,以告知呼叫来自何处或任何其他数据?

标签: javascript jquery


【解决方案1】:

您可以直接使用.ajax() 发送标头。来自文档:

jQuery.getScript()是一个简写的Ajax函数,相当于:

$.ajax({
    url: url,
    dataType: 'script',
    success: function(data){
        console.log(data);
    }
});

从这里,我们可以使用headers 设置,我们会得到这样的结果:

$.ajax({
    url: url,
    dataType: 'script',
    headers: {'X-Alt-Referer': location.href },
    success: function(data){
        console.log(data);
    }
});

This answer也可以帮到你。

【讨论】:

    【解决方案2】:

    你可以这样做

    function GetScript(url)
    {
         $.getScript(url)
         .done(function(script, textStatus)
         {
             console.log( textStatus );
         })
         .fail(function( jqxhr, settings, exception ) {
             console.log(exception);
             return GetScript("http://www.example.org/file_2.js");
         });
    }
    
    GetScript("http://www.example.org/file.js");
    

    【讨论】:

    • 谢谢,但是你在这个函数中在哪里添加referer?
    • 我认为您将 deferredreferer 混淆了。
    猜你喜欢
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多