在重定向之前在HTTP中添加自定义标头 [英] Adding custom header in HTTP before redirect
问题描述
是否有任何方法可以在由javascript触发的HTTP重定向中添加自定义HTTP标头?
Is there any way to add a custom HTTP header in a HTTP redirect triggered by javascript?
我正在寻找可以将客户标头元素添加到任何重定向的内容.
I'm looking for something that would add a customer header element to any redirect.
有一种方法可以处理AJAX请求:
如何我将自定义HTTP标头添加到使用js或jQuery的ajax请求中吗?
There is a way to do it with AJAX requests:
How can I add a custom HTTP header to ajax request with js or jQuery?
推荐答案
我想您的意思是通过window.location
进行重定向.
I suppose you mean redirecting by window.location
.
无论使用哪种语言或框架,都无法重定向到设置了自定义标题的页面.换句话说,无法触发HTTP重定向并导致客户端添加自定义标头.
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请求的唯一方法是使用XMLHttpRequest
对象.并且它需要在目标服务器上实现的CORS才能允许此类Ajax请求.
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. And it needs CORS implemented on the target server to allow such ajax requests.
您能想到的最接近的解决方法是充当代理.例如,发送设置了自定义HTTP标头的ajax请求,检索响应并将其注入到DOM中,这是基于错误思想的一种情况.
The closest workaround you can come up with would be to act as a proxy. e.g., 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中添加自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!