changing the referrer of an Ajax POST

后端 未结 4 912
[愿得一人]
[愿得一人] 2020-12-01 13:58

Anyone knows if with jquery or general javascript, I can change the referrer from the header in an http ajax call?

basically I want it to be sent from my page but ha

相关标签:
4条回答
  • 2020-12-01 14:33

    You can't do that with jQuery, but you CAN do that with fetch

    Not sure if it would work for cross domain requests (you will obviously need at least CORS permissions for that) but it definitely does work for same domain + different page like in this example

    fetch("http://example.com",{"referrer":"http://example.com/inbox","body":"{\"format\":\"root\"}","method":"POST"});
    
    0 讨论(0)
  • 2020-12-01 14:37

    You can always use this :

    jQuery.ajaxSetup({
        'beforeSend': function(xhr) {xhr.setRequestHeader("header key", "header value")}
    })
    

    But ofcourse, the browser can have a different opinion about the referer header. This should be tested :)

    0 讨论(0)
  • 2020-12-01 14:48

    You can use .setRequestHeader( 'referer', 'foo' ), but I'm not sure if the browser would just replace that with the proper one or not.

    via jQuery, the .ajax() method allows headers as well (.get() and .post() don't)

    Note that there's very little point to doing this as you can't do cross-domain AJAX and even attempting to do this could possibly trigger XHR security rules in some browsers and just stop the request altogether.

    0 讨论(0)
  • 2020-12-01 14:59

    The browser will overwrite the referrer always for the tests that I've done. Meaning you can't change the referrer of an ajax call.

    0 讨论(0)
提交回复
热议问题