gpt4 book ai didi

javascript - GET 到 POST 不工作

转载 作者:可可西里 更新时间:2023-11-01 16:38:12 24 4
gpt4 key购买 nike

我想发出请求以发布以下代码的请求 -

       <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。请让我知道还需要做什么。

感谢您的帮助!

最佳答案

您实际上是在使用 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。位置

关于javascript - GET 到 POST 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19059608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com