1

I fetch script with

$.getScript("http://www.example.org/file.js");

However, the referer goes empty to that site. How can I use referer (can be antyhing) instead of empty referer ? Note: Using getscript is required, I cant use script src="".

1
  • What are you trying to do exactly? Send a header through AJAX to the target site to inform from where the call comes or any other data? Commented Aug 3, 2015 at 18:13

2 Answers 2

3

You can send headers using directly .ajax(). From documentation:

jQuery.getScript() is a shorthand Ajax function, which is equivalent to:

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

From here, we can use the headers setting and we get something like this:

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

This answer can help you too.

Sign up to request clarification or add additional context in comments.

Comments

2

You can do it like this

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

2 Comments

Thanks, but where do you add referer in this function ?
I think you confused deferred with referer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.