The jQuery `submit` method ignores POST messages with GET. What for?

I have such a form.

<form id="hiddenForm" method="post" action="/my-controller/my-action/" enctype="multipart/form-data">
    <input type="hidden" id="HiddenFormInput" name="HiddenFormInput">
</form>

which is created by a web page accessible through https://[my-server]/my-controller/my-action/, which is also the action that the form points to.

On the server side, I distinguish between requests GETand POST. Either the page is displayed explicitly ( GET), or some results are displayed depending on the values ​​of the form ( POST).

When I use jQuery to submit the form (I DO NOT use AJAX here, just submitting the form), as in this snippet,

$("#hiddenForm").submit();

then jQuery seems to submit this form using GET, although it is explicitly marked for submit using POST. Even alert($("#hiddenForm").attr("method"));always gives "post". However, this unwanted behavior only occurs if the page has been loaded with GET. If the page is specified with POST, i.e. The form has already been submitted at least once, everything works as expected.

Update

The problem was plain old preventDefault(). The button that initiated the sending had its own functionality. It was a hyperlink. Therefore, I had this chain of actions.

  • Hyperlink to https://[my-server]/my-controller/my-action/#
  • Submit a form with an action https://[my-server]/my-controller/my-action/.

, GET . https://[my-server]/my-controller/my-action/#, . , .

  • https://[my-server]/my-controller/my-action/# ( , )
  • https://[my-server]/my-controller/my-action/.

, .

+4
1

, , , jQuery 2.0.3, :

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
  $('#dosubmit').on('click',function(){
    $("#hiddenForm").submit();
  });
});
</script>
</head>
<body>

<?php
//
if(count($_GET) > 0) {
  echo "<textarea style=\"width: 700px; height: 90px;\">
Submitted with method=GET: \$_GET:
";
  var_dump($_GET);
  echo "</textarea>";
}

if(count($_POST) > 0) {
echo "<textarea style=\"width: 700px; height: 90px;\">
Submitted with method=POST: \$_POST:
";
  var_dump($_POST);
  echo "</textarea>";
}
?>

<form id="hiddenForm" method="post" enctype="multipart/form-data">
  <input type="hidden" id="HiddenFormInput" name="HiddenFormInput" />
</form>

<div id="dosubmit" style="background-color: gold; width: 200px; cursor: pointer;">Click me to Do jQuery Submit</div>

</body>
</html>

, $( "# hiddenForm" ). submit(); .

:

Submitted with method=POST: $_POST:
array(1) {
  ["HiddenFormInput"]=>
    string(0) ""
}

URL- , . name= " HiddenFormInput" .

0

All Articles