Javascript jQuery`submit`方法忽略带有GET的POST发送。为什么?
我有一张这样的表格Javascript jQuery`submit`方法忽略带有GET的POST发送。为什么?,javascript,php,jquery,forms,zend-framework,Javascript,Php,Jquery,Forms,Zend Framework,我有一张这样的表格 <form id="hiddenForm" method="post" action="/my-controller/my-action/" enctype="multipart/form-data"> <input type="hidden" id="HiddenFormInput" name="HiddenFormInput"> </form> 然后jQuery似乎用GET提交此表单,尽管它清楚地标记为用POST提交。甚至是警
<form id="hiddenForm" method="post" action="/my-controller/my-action/" enctype="multipart/form-data">
<input type="hidden" id="HiddenFormInput" name="HiddenFormInput">
</form>
然后jQuery似乎用GET
提交此表单,尽管它清楚地标记为用POST
提交。甚至是警报($(“#hiddenForm”).attr(“方法”)代码>总是产生“post”
。但是,只有当页面已由GET
加载时,才会发生这种不必要的行为。如果页面由帖子提交,即表单至少已提交一次,则一切正常
更新
问题是一个普通的老问题。触发提交的按钮有自己的功能。这是一个超链接。所以我采取了一连串的行动
超链接到https://[my server]/my controller/my 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/
然后它当然起作用了。正如评论中所讨论的,没有bug,至少在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>
在原始表单中,提交不会传递URL参数,因为只提交指定了name属性的输入。因此,我为隐藏的输入添加了属性name=“HiddenFormInput”。document.getElementById(“hiddenForm”).submit()的作用是什么do?你怎么知道该页面是使用
GET
方法提交的?你是否尝试将
元素添加到表单中?我测试了它,提交方法“POST”由jQuery使用。感谢preventDefault()
,我也遇到过,需要咖啡:)
<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>
Submitted with method=POST: $_POST:
array(1) {
["HiddenFormInput"]=>
string(0) ""
}