带有 HTTP 标头的 redirect_to [英] redirect_to with HTTP header

查看:65
本文介绍了带有 HTTP 标头的 redirect_to的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定这是否可能或是否有意义,但我正在尝试在设置 HTTP 标头字段后进行重定向.类似的东西:

Not sure if this is possible or makes any sense, but I'm trying to make a redirect after setting a HTTP header field. Something like:

request.headers['Accept'] = 'application/json'
redirect_to url

显然这不起作用,因为 request 是当前请求,而不是重定向.有没有办法使用自定义 HTTP 标头调用 redirect_to ?

Obviously this doesn't work, as request is the current request, not the redirect. Is there any way to call redirect_to with custom HTTP headers?

推荐答案

无论您使用什么语言或框架,都不可能重定向到设置了自定义标题的页面.换句话说,无法触发 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 (browser) to add a custom header.

正如您自己提到的,您正在为指示浏览器重定向的响应设置自定义标头,而不是为重定向本身设置.

As you yourself mentioned, you're setting the custom headers for the response which is instructing the browser to redirect, not for the redirect itself.

网站指示浏览器发出带有自定义标头的 HTTP 请求的唯一方法是使用 Javascript 和 XMLHttpRequest 对象.

The only way for a site to instruct a browser to issue an HTTP request with a custom header is to use Javascript and the XMLHttpRequest object.

这篇关于带有 HTTP 标头的 redirect_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆