我想提出请求以发布以下代码的请求 -
<html>
<head>
<script language="JavaScript">
function redirect() {
if (window.focus)
self.focus();
this.location = '/test/GetReports?id=
<%=System.currentTimeMillis()+session.getId()%>';
}
</script>
<title>Downloading Document</title>
</head>
<body marginwidth='0' marginheight='0' onload='javascript:redirect()'>
</body>
</html>
为实现这一点,我在下面做了 -
<html>
<head>
<script language="JavaScript">
function redirect() {
if (window.focus)
self.focus();
location = '/test/GetReports?id=<%=System.currentTimeMillis()+session.getId()%>';
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", location);
form.submit();
}
</script>
<title>Downloading Document</title>
</head>
<body marginwidth='0' marginheight='0' onload='javascript:redirect()'>
</body>
</html>
但它没有任何区别。这个请求仍然是Get。请告诉我还有什么需要做的。
感谢您的帮助!
答案 0 :(得分:1)
您实际上是使用POST表单发送GET数据。 URL中指定的参数始终被视为GET数据。
为了说明这意味着什么,如果您有一个如下所示的表单:
<form method="post" action="test.html?gid=1">
<input name="pid" value="2">
</form>
...服务器将获得值gid
作为GET,pid
作为POST。无论表单使用什么方法,URL中的任何内容都将作为GET发送。
因此,要使代码正常工作,您需要在表单中创建一个字段,以便将数据作为POST发送。
function redirect() {
if (window.focus) self.focus();
loc = '/test/GetReports'; // note: no parameters here
var form = document.createElement("form");
// create the input element
var input = document.createElement("input");
input.setAttribute("name", "id");
input.setAttribute("value", <%=System.currentTimeMillis()+session.getId()%>);
form.appendChild(input);
form.setAttribute("method", "post");
form.setAttribute("action", loc);
form.submit();
}
另请注意,我将location
重命名为loc
- 否则您可能会遇到问题,因为location
会指向window.location
。