Html 重定向到另一个 URL 时如何传递 URL 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31603974/
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
How to pass URL parameters when redirecting to another URL?
提问by lukas.coenig
I'm using this html code to redirect to another URL when the page (www.dasinfobuch.de/links/Wizz) is invoked:
当页面 (www.dasinfobuch.de/links/Wizz) 被调用时,我使用这个 html 代码重定向到另一个 URL:
<head>
<meta http-equiv="refresh" content="0; URL=http://52.28.104.181:8080/Wizard/Wizz">
</head>
However, when I use a URL parameter such as
但是,当我使用 URL 参数时,例如
www.dasinfobuch.de/links/Wizz?template=test
the parameter is not passed on to the redirected page. Is there a way to accomplish this (preferably in plain HTML)? (I'm new to Web programming.)
参数不会传递到重定向页面。有没有办法做到这一点(最好是纯 HTML)?(我是 Web 编程的新手。)
回答by dakab
This is not possible using only a meta element that mimics the non-standard Refresh
HTTP header field. Of course there are other ways.
仅使用模仿非标准Refresh
HTTP 标头字段的元元素是不可能的。当然还有其他方法。
If you've got something like a preprocessor, you can pass on the request to the HTML, like so:
如果您有类似预处理器的东西,您可以将请求传递给 HTML,如下所示:
<meta http-equiv="refresh"
content="0; URL=http://52.28.104.181:8080/Wizard/Wizz?template=<%
out.print(request.getParameter("template")) %>">
Another (client-side) way is to redirect using JavaScript:
另一种(客户端)方式是使用 JavaScript 重定向:
document.location.href = 'http://52.28.104.181:8080/Wizard/Wizz' + document.location.search;
Note that this will carry over the entire query string, not just the template
parameter and its argument. If that's a problem, it's easy to get only the desired string from location.search
.
请注意,这将保留整个查询字符串,而不仅仅是template
参数及其参数。如果这是一个问题,很容易从location.search
.