简体   繁体   English

不带参数发送表格POST

[英]Form POST being sent with no parameters

I have a very simple HTML page: 我有一个非常简单的HTML页面:

<html>
<head>
    <script>
        function post() {   
            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", "/agree");

            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "location");
            hiddenField.setAttribute("value", "URL goes here");

            document.body.appendChild(form);
            form.submit();
        }
    </script>
</head>

<body>

<input type="button" value="I agree" onclick="post()">

</body>

For whatever reason, both in the Firefox network inspector and in the HTTPServlet code handling the post, there are no parameters, eg in the network inspector it says "No parameters for this request", and in Java using getParameter or getParameterNames both return null. 无论出于什么原因,无论是在Firefox网络检查器中还是在处理该帖子的HTTPServlet代码中,都没有参数,例如,在网络检查器中它说“此请求没有参数”,在Java中使用getParameter或getParameterNames都返回null。

I have tried changing the form of the JS a little bit, to see another format I often see being used for adding attributes. 我试图稍微更改JS的形式,以查看我经常看到的用于添加属性的另一种格式。 For example: 例如:

hiddenField.type = "hidden";

But this changes nothing (what's the difference anyway?). 但这并没有改变(仍然有什么区别?)。 What am I doing wrong? 我究竟做错了什么?

You forgot to append the hiddenField to the form: 您忘了将hiddenField追加到表单:

form.appendChild(hiddenField);

Small side note: there is missing a double quote here (before /agree ): 小旁注:这里( /agree之前)缺少双引号:

form.setAttribute("action", "/agree");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM