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

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

  • <small id='xglvD'></small><noframes id='xglvD'>

    <legend id='xglvD'><style id='xglvD'><dir id='xglvD'><q id='xglvD'></q></dir></style></legend><tfoot id='xglvD'></tfoot>

      1. FormData() 对象不会从表单添加提交类型的输入,而在 Firefox 上

        FormData() object does not add submit-type inputs from form, while on Firefox(FormData() 对象不会从表单添加提交类型的输入,而在 Firefox 上)

            <tfoot id='mleqn'></tfoot>
            1. <small id='mleqn'></small><noframes id='mleqn'>

                • <bdo id='mleqn'></bdo><ul id='mleqn'></ul>

                    <tbody id='mleqn'></tbody>
                • <i id='mleqn'><tr id='mleqn'><dt id='mleqn'><q id='mleqn'><span id='mleqn'><b id='mleqn'><form id='mleqn'><ins id='mleqn'></ins><ul id='mleqn'></ul><sub id='mleqn'></sub></form><legend id='mleqn'></legend><bdo id='mleqn'><pre id='mleqn'><center id='mleqn'></center></pre></bdo></b><th id='mleqn'></th></span></q></dt></tr></i><div id='mleqn'><tfoot id='mleqn'></tfoot><dl id='mleqn'><fieldset id='mleqn'></fieldset></dl></div>
                • <legend id='mleqn'><style id='mleqn'><dir id='mleqn'><q id='mleqn'></q></dir></style></legend>
                  本文介绍了FormData() 对象不会从表单添加提交类型的输入,而在 Firefox 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  今天我遇到了一个有趣的错误,花了很多时间才弄清楚.

                  设置

                  页面上的表单.提交时,数据被捕获并用它创建 new FormData() 对象.

                  该对象通过 xhr 请求发送到 .php 脚本,然后返回 ok/error 消息.

                  代码看起来像这样:(简化版,不需要绒毛)

                  将其发送到 .php 将产生一个如下所示的数组:

                  数组([名称] =>一些名字[电子邮件] =>一些电子邮件[电话] =>11111111[网站] =>一些网站[详情] =>一些细节[发送] =>发送)

                  .php 将响应 {"message":"ok","code":0}{"message":"error","代码":1}

                  现在这是预期的行为.这是我在 Chrome、IE 或 Safari 上得到的.

                  问题

                  然而,在 Firefox 上,除了没有 submit 输入 (name="send") 键/值对之外,我得到相同的数组:

                  数组([名称] =>一些名字[电子邮件] =>一些电子邮件[电话] =>11111111[网站] =>一些网站[详情] =>一些细节)

                  我在 Linux 和 Windows 上都尝试过,以覆盖我的基础,但它仍然给出了同样不令人满意的结果.

                  解决方案

                  在网上搜索并出现空白后,我解决它的方法(更多的补丁,而不是真正解决)是覆盖 send 键/值:

                  var data = new FormData(frm);data.append('发送', '发送');xhr.send(数据);

                  这是可行的,因为如果它已经定义(Chrome 等......)它会被覆盖,如果它不存在,它就会被创建.

                  问题

                  1. 类似情况 - 您是否遇到过类似情况?
                  2. 修复 - 我认为我的解决方案是 hack,您有什么更好的修复方法吗?

                  解决方案

                  根据 WHATWG 规范,FireFox 似乎是正确的.

                  FormDataXMLHttpRequest 规范> 构造函数说:

                  <块引用>

                  1. 如果给出 form,则将 fd 的条目设置为 构造form 的表单数据集.

                  那么在构造表单数据集的描述中,是这样说的:

                  <块引用>

                  在提交者提交者的上下文中form可选地构造表单数据集的算法如下.如果没有另外指定,提交者为空.

                  表单中的按钮只有在提交者的情况下才会包含在表单数据集中.但是当这个算法从 FormData 构造函数中执行时,没有指定提交者,所以表单数据集中不应该包含任何按钮.

                  Today I came across an interesting bug, which took a good chunk of time to get to the bottom of.

                  The setup

                  A form on a page. On submit, the data gets captured and new FormData() object gets created with it.

                  That object gets sent with and xhr request to an .php script, which then returns an ok / error message.

                  The code looks something like this: (simplified version, no need for fluff)

                  <form name="frm" id="frm" action="" method="post" onsubmit="save(event, this);" enctype="multipart/form-data">
                      <input name="name" id="name" type="text" value="..." />
                      <input name="email" id="email" type="text" value="..." />
                      <input name="phone" id="phone" type="text" value="..." />
                      <input name="website" id="website" type="text" value="..." />
                      <textarea name="details" id="details"></textarea>
                      <input name="send" type="submit" value="Send" />
                  </form>
                  
                  <script type="text/javascript">
                  
                  function save(e, frm) {
                  
                          if (document.getElementById('nume').value == '' ||
                            document.getElementById('email').value == '' ||
                            document.getElementById('telefon').value == '' ||
                            document.getElementById('site').value == '') {
                  
                              alert('Forms empty');
                  
                          } else {
                  
                              var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
                  
                              xhr.onreadystatechange = function () {
                                  if (xhr.readyState == 4) {
                  
                                      var r = JSON.parse(xhr.responseText);
                  
                                      if (r.code == 0) {
                                          document.getElementById('message_ok').style.display = 'block';
                                      } else {
                                          document.getElementById('message_err').style.display = 'block';
                                      }
                                  }
                              };
                  
                              xhr.open('POST', 'http://url', true);
                              var data = new FormData(frm);
                              xhr.send(data);
                  
                          }
                      e.preventDefault();
                  }
                  
                  </script>
                  

                  Sending this to .php will result in an array which kind of looks like this:

                  Array
                  (
                      [name] => some name
                      [email] => some email
                      [phone] => 11111111
                      [website] => some site
                      [details] => some details
                      [send] => Send
                  )
                  

                  and .php will respond with either {"message":"ok","code":0} or {"message":"error","code":1}

                  Now this is the expected behavior. This is what I get on either Chrome, IE or Safari.

                  The problem

                  On Firefox however, I get the same array except without the submit input (name="send") key/value pair:

                  Array
                  (
                      [name] => some name
                      [email] => some email
                      [phone] => 11111111
                      [website] => some site
                      [details] => some details
                  )
                  

                  I tried on both Linux and Windows, to cover my basis, yet it still gave the same unsatisfying result.

                  Solution

                  After searching online and coming up empty, the way I solved it (more of patching, not really solving) was to overwrite the send key/value:

                  var data = new FormData(frm);
                  data.append('send', 'Send');
                  xhr.send(data);
                  

                  This works, because if it's already defined (Chrome, etc...) it gets overwritten, if it doesn't exist, it gets created.

                  Questions

                  1. Similar - Have you ever faced something similar?
                  2. Fix - I consider my solution a hack, have you got any ideas for a better fix?

                  解决方案

                  FireFox seems to be correct, according to the WHATWG specification.

                  The XMLHttpRequest specification of the FormData constructor says:

                  1. If form is given, set fd's entries to the result of constructing the form data set for form.

                  Then in the description of constructing the form data set, it says:

                  The algorithm to construct the form data set for a form form optionally in the context of a submitter submitter is as follows. If not specified otherwise, submitter is null.

                  A button in the form is only included in the form data set if it's the submitter. But when this algorithm is executed from the FormData constructor, no submitter is specified, so no buttons should be included in the form data set.

                  这篇关于FormData() 对象不会从表单添加提交类型的输入,而在 Firefox 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
                  XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
                  Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                  How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
                  quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
                  How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)
                  <tfoot id='744ik'></tfoot>
                • <legend id='744ik'><style id='744ik'><dir id='744ik'><q id='744ik'></q></dir></style></legend>
                  <i id='744ik'><tr id='744ik'><dt id='744ik'><q id='744ik'><span id='744ik'><b id='744ik'><form id='744ik'><ins id='744ik'></ins><ul id='744ik'></ul><sub id='744ik'></sub></form><legend id='744ik'></legend><bdo id='744ik'><pre id='744ik'><center id='744ik'></center></pre></bdo></b><th id='744ik'></th></span></q></dt></tr></i><div id='744ik'><tfoot id='744ik'></tfoot><dl id='744ik'><fieldset id='744ik'></fieldset></dl></div>
                    <bdo id='744ik'></bdo><ul id='744ik'></ul>
                      1. <small id='744ik'></small><noframes id='744ik'>

                              <tbody id='744ik'></tbody>