简体   繁体   English

在重定向之前在HTTP中添加自定义标头

[英]Adding custom header in HTTP before redirect

Is there any way to add a custom HTTP header in a HTTP redirect triggered by javascript? 有什么方法可以在由javascript触发的HTTP重定向中添加自定义HTTP标头?

I'm looking for something that would add a customer header element to any redirect. 我正在寻找可以将客户标头元素添加到任何重定向的东西。

There is a way to do it with AJAX requests: 有一种方法可以处理AJAX请求:
How can I add a custom HTTP header to ajax request with js or jQuery? 如何使用js或jQuery向ajax请求添加自定义HTTP标头?

I suppose you mean redirecting by window.location . 我想你的意思是通过window.location重定向。

It's impossible to redirect to a page with custom headers set, no matter what language or framework you use. 无论您使用哪种语言或框架,都不可能重定向到设置了自定义标题的页面。 In other words, there's no way to trigger an HTTP redirect and cause the client to add a custom header. 换句话说,无法触发HTTP重定向并导致客户端添加自定义标头。

The only way for a site to instruct a browser to issue an HTTP request with a custom header is to use the XMLHttpRequest object. 网站指示浏览器发出带有自定义标头的HTTP请求的唯一方法是使用XMLHttpRequest对象。 And it needs CORS implemented on the target server to allow such ajax requests. 而且它需要在目标服务器上实现CORS才能允许此类Ajax请求。

The closest workaround you can come up with would be to act as a proxy. 您能想到的最接近的解决方法是充当代理。 eg, Send ajax requests with custom HTTP headers set, retrieve the response and inject it into the DOM which is a case for building upon the wrong idea. 例如,发送带有自定义HTTP标头设置的ajax请求,检索响应并将其注入DOM,这是基于错误思想的一种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM