通过$.getscript()获取脚本时添加自定义引用器

Add custom referer when fetching script via $.getscript()

本文关键字:添加 自定义 引用 脚本 获取 getscript 通过      更新时间:2023-09-26

我用

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

但是,指向该站点的引用是空的。我如何使用referer(可以是任何东西)而不是空referer ?注意:使用getscript是必需的,我不能使用script src=".

您可以直接使用.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);
    }
});

这个答案也可以帮助你。

你可以这样做

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