简体   繁体   中英

Form POST being sent with no parameters

I have a very simple HTML page:

<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.

I have tried changing the form of the JS a little bit, to see another format I often see being used for adding attributes. 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:

form.appendChild(hiddenField);

Small side note: there is missing a double quote here (before /agree ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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