<bdo id='KV8dv'></bdo><ul id='KV8dv'></ul>

    <tfoot id='KV8dv'></tfoot>
  1. <legend id='KV8dv'><style id='KV8dv'><dir id='KV8dv'><q id='KV8dv'></q></dir></style></legend>

  2. <i id='KV8dv'><tr id='KV8dv'><dt id='KV8dv'><q id='KV8dv'><span id='KV8dv'><b id='KV8dv'><form id='KV8dv'><ins id='KV8dv'></ins><ul id='KV8dv'></ul><sub id='KV8dv'></sub></form><legend id='KV8dv'></legend><bdo id='KV8dv'><pre id='KV8dv'><center id='KV8dv'></center></pre></bdo></b><th id='KV8dv'></th></span></q></dt></tr></i><div id='KV8dv'><tfoot id='KV8dv'></tfoot><dl id='KV8dv'><fieldset id='KV8dv'></fieldset></dl></div>
  3. <small id='KV8dv'></small><noframes id='KV8dv'>

      jQuery `submit` 方法忽略使用 GET 发送的 POST.为什么?

      时间:2023-05-30
      <tfoot id='mwhmI'></tfoot>
      • <bdo id='mwhmI'></bdo><ul id='mwhmI'></ul>
          <tbody id='mwhmI'></tbody>
        1. <small id='mwhmI'></small><noframes id='mwhmI'>

          <i id='mwhmI'><tr id='mwhmI'><dt id='mwhmI'><q id='mwhmI'><span id='mwhmI'><b id='mwhmI'><form id='mwhmI'><ins id='mwhmI'></ins><ul id='mwhmI'></ul><sub id='mwhmI'></sub></form><legend id='mwhmI'></legend><bdo id='mwhmI'><pre id='mwhmI'><center id='mwhmI'></center></pre></bdo></b><th id='mwhmI'></th></span></q></dt></tr></i><div id='mwhmI'><tfoot id='mwhmI'></tfoot><dl id='mwhmI'><fieldset id='mwhmI'></fieldset></dl></div>

                <legend id='mwhmI'><style id='mwhmI'><dir id='mwhmI'><q id='mwhmI'></q></dir></style></legend>
                本文介绍了jQuery `submit` 方法忽略使用 GET 发送的 POST.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个这样的表格.

                <input type="hidden" id="HiddenFormInput" name="HiddenFormInput"></表单>

                由可通过 https://[my-server]/my-controller/my-action/ 访问的网页生成,这也是表单指向的操作.>

                在服务器端,我区分了 GETPOST 请求.页面显示清晰 (GET) 或根据表单的值显示某些结果 (POST).

                当我使用 jQuery 提交表单时(我在这里没有使用 AJAX,只是提交表单),如本代码段所示,

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

                然后 jQuery 似乎是用 GET 提交这个表单的,尽管它清楚地标记为用 POST 提交.即使是 alert($("#hiddenForm").attr("method")); 也总是产生 "post".但是,只有当页面已被 GET 加载时,才会发生这种不需要的行为.如果页面是通过 POST 来的,即表单至少已经提交过一次,那么一切都按预期进行.

                更新

                问题是一个普通的 preventDefault() 问题.触发提交的按钮有自己的功能.那是一个超链接.所以我有了这个动作链.

                1. 超链接到https://[my-server]/my-controller/my-action/#
                2. 使用操作https://[my-server]/my-controller/my-action/提交表单.

                当然,这会导致在没有提交表单的情况下调用 GET.一旦我在页面 https://[my-server]/my-controller/my-action/# 上,超链接失去了它的功能.这就是为什么第二次尝试总是有效的原因,因为操作链减少到提交表单.

                1. 超链接到 https://[my-server]/my-controller/my-action/#(无效,因为已经存在)
                2. 使用操作https://[my-server]/my-controller/my-action/提交表单.

                然后它当然起作用了.

                解决方案

                正如评论中所讨论的,没有错误,至少在 jQuery 2.0.3 的最后一个版本上,可以用这个来测试:

                <头><script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script><脚本>$(document).ready(function () {$('#dosubmit').on('click',function(){$("#hiddenForm").submit();});});<身体><?php//如果(计数($_GET)> 0){echo "<textarea style="width: 700px;高度:90px;">提交方法=GET:$_GET:";var_dump($_GET);echo "</textarea>";}如果(计数($_POST)> 0){echo "<textarea style="width: 700px;高度:90px;">提交方法=POST:$_POST:";var_dump($_POST);echo "</textarea>";}?><form id="hiddenForm" method="post" enctype="multipart/form-data"><input type="hidden" id="HiddenFormInput" name="HiddenFormInput"/></表单><div id="dosubmit" style="background-color: gold; width: 200px; cursor: pointer;">点击我做jQuery提交</div>

                当点击金色背景文字时,$("#hiddenForm").submit();用于提交表单.

                这是输出:

                提交方法=POST:$_POST:数组(1){["HiddenFormInput"]=>字符串(0)"}

                在您的原始表单中,提交不会传递任何 URL 参数,因为仅提交具有指定 name 属性的输入.所以我为你的隐藏输入添加了属性 name="HiddenFormInput".

                I have a form like this.

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

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

                On the server side, I differentiate between GET and POST requests. Either the page is shown plainly (GET) or some results are shown depending on the form's values (POST).

                When I use jQuery to submit the form (I am NOT employing AJAX here, simply submitting the form), as in this snippet,

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

                then jQuery seems to submit this form with GET, although it's clearly marked to be submitted with POST. Even an alert($("#hiddenForm").attr("method")); always yields "post". However, this unwanted behaviour only occurs if the page has been loaded by a GET. If the page came by a POST, i.e. the form has already been submitted at least once, everything works just as expected.

                Update

                The problem was a plain old preventDefault() one. The button that triggered the submit had a functionality of its own. It was a hyperlink. So I had this chain of actions.

                1. Hyperlink to https://[my-server]/my-controller/my-action/#
                2. Submit form with action https://[my-server]/my-controller/my-action/.

                Of course that would result in a GET call without the form being submitted. Once I was on the page https://[my-server]/my-controller/my-action/# the hyperlink lost it's functionality. That's why the second try always worked because the chain of actions was reduced to the submission of the form.

                1. Hyperlink to https://[my-server]/my-controller/my-action/# (no effect because already there)
                2. Submit form with action https://[my-server]/my-controller/my-action/.

                Then it worked of course.

                解决方案

                As discussed in comments, there is no bug, at least on the last version of jQuery 2.0.3, one can test with this:

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

                When one clicks on gold background text, $("#hiddenForm").submit(); is used to submit the form.

                Here is the output:

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

                In your original form, no URL arguments are passed by the submitting, as only inputs with name attribute specified are submitted. So I added the attribute name="HiddenFormInput" for your hidden input.

                这篇关于jQuery `submit` 方法忽略使用 GET 发送的 POST.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:文件名 feed1.xls 在 php 中不可读 下一篇:如何检查我的 Apache 安装中是否启用了 PDO 支持?

                相关文章

                <i id='26Pw6'><tr id='26Pw6'><dt id='26Pw6'><q id='26Pw6'><span id='26Pw6'><b id='26Pw6'><form id='26Pw6'><ins id='26Pw6'></ins><ul id='26Pw6'></ul><sub id='26Pw6'></sub></form><legend id='26Pw6'></legend><bdo id='26Pw6'><pre id='26Pw6'><center id='26Pw6'></center></pre></bdo></b><th id='26Pw6'></th></span></q></dt></tr></i><div id='26Pw6'><tfoot id='26Pw6'></tfoot><dl id='26Pw6'><fieldset id='26Pw6'></fieldset></dl></div>

              1. <tfoot id='26Pw6'></tfoot>

                <small id='26Pw6'></small><noframes id='26Pw6'>

                <legend id='26Pw6'><style id='26Pw6'><dir id='26Pw6'><q id='26Pw6'></q></dir></style></legend>
                • <bdo id='26Pw6'></bdo><ul id='26Pw6'></ul>