Javascript 更改 Ajax POST 的引用者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8231366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:13:59  来源:igfitidea点击:

changing the referrer of an Ajax POST

javascriptajaxjqueryhttp-headers

提问by fmsf

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

任何人都知道是否使用 jquery 或通用 javascript,我可以在 http ajax 调用中从标头更改引荐来源网址?

basically I want it to be sent from my page but have a referrer from another page. Any information would be great.

基本上我希望它从我的页面发送,但有来自另一个页面的引用。任何信息都会很棒。

回答by fmsf

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.

对于我所做的测试,浏览器将始终覆盖引用者。这意味着您无法更改 ajax 调用的引用。

回答by Mark Kahn

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

您可以使用.setRequestHeader( 'referer', 'foo' ),但我不确定浏览器是否会将其替换为正确的。

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

通过 jQuery,该.ajax()方法也允许标头(.get()并且.post()不允许)

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.

请注意,这样做没有什么意义,因为您不能进行跨域 AJAX,甚至尝试这样做也可能会在某些浏览器中触发 XHR 安全规则并完全停止请求。

回答by Radoslav Georgiev

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 :)

但是当然,浏览器可以对引用标头有不同的看法。这应该被测试:)

回答by dmitry.matora

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

你不能做到这一点与jQuery的,但你的CAN做到这一点与取

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

不确定它是否适用于跨域请求(您显然至少需要 CORS 权限)但它确实适用于同一个域+不同的页面,如本例所示

fetch("http://example.com",{"referrer":"http://example.com/inbox","body":"{\"format\":\"root\"}","method":"POST"});