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

      2. <small id='Ai5gk'></small><noframes id='Ai5gk'>

          <bdo id='Ai5gk'></bdo><ul id='Ai5gk'></ul>
        <legend id='Ai5gk'><style id='Ai5gk'><dir id='Ai5gk'><q id='Ai5gk'></q></dir></style></legend>

        XMLHttpRequest &amp;FormData 未提交数据

        时间:2024-08-10
      3. <tfoot id='FWrr8'></tfoot>

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

                <small id='FWrr8'></small><noframes id='FWrr8'>

                  <tbody id='FWrr8'></tbody>
                  <bdo id='FWrr8'></bdo><ul id='FWrr8'></ul>
                • 本文介绍了XMLHttpRequest &amp;FormData 未提交数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 post 方法和 FormData 对象通过 ajax 提交表单.

                  I am trying to submit a form via ajax using the post method and a FormData object.

                  这是 JavaScript 的简化版本:

                  Here is a simplified version of the JavaScript:

                  var form=…; //  form element
                  var url=…;  //  action
                  form['update'].onclick=function(event) {    //  button name="update"
                      var xhr=new XMLHttpRequest();
                          xhr.open('post',url,true);
                          xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                      var formData=new FormData(form);
                          formData.append('update', true);    // makes no difference
                      xhr.send(formData);
                      xhr.onload=function() {
                          alert(this.response);
                      };
                  };
                  

                  表格有:

                  • 运行脚本的按钮(type=button"name=update")
                  • 没有 actionmethod="get"

                  我的 PHP 脚本有以下内容:

                  My PHP script has the following:

                  if(isset($_POST['update'])) {
                      print_r($_POST);
                      exit;
                  }
                  
                  //  more stuff
                  
                  print 'other stuff';
                  

                  当我尝试它时,PHP 会进入其余代码,我得到另一个输出,而不是我对 print_r 语句的期望.

                  When I try it, the PHP falls through to the rest of the code, and I get the other output, rather than what I expect from the print_r statement.

                  我尝试了以下变体:

                  • new FormData()(没有表单).如果我手动添加 update 数据,这确实工作.
                  • 新的 FormData(form).这不起作用,无论我是否手动添加 update.
                  • 将表单方法更改为post.
                  • Firefox、Safari 和MacOS 上的 Chrome;所有当前版本.
                  • new FormData() (without the form). This does work if I add the update data manually.
                  • new FormData(form). This does not work, whether I add the update manually or not.
                  • changing the form method to post.
                  • Firefox, Safari & Chrome on MacOS; all current versions.

                  from 本身看起来像这样:

                  The from itself looks something like this:

                  <form id="edit" method="post" action="">
                      <p><label for="edit-summary">Summary</label><input id="edit-summary" name="summary" type="text"></p>
                      <p><label for="edit-description">Description</label><input id="edit-description" name="description" type="text"></p>
                      <p><label for="edit-ref">Reference</label><input id="edit-ref" name="ref" type="text"></p>
                      <p><label for="edit-location">Location</label><input id="edit-location" name="location" type="text"></p>
                      <p><button type="button" name="update">OK</button></p>
                  </form>
                  

                  我应该怎么做才能提交让它工作?

                  What should I do to submit the get this to work?

                  请不要使用 jQuery.

                  No jQuery, please.

                  推荐答案

                  发送FormData对象时的内容类型是multipart/form-data 不是url编码的.
                  此外,必须为请求设置适当的边界,这是用户无法做到的.为此 XMLHttpRequest 设置具有所需边界的正确内容类型.
                  因此,您所要做的就是不设置内容类型,它会起作用.

                  The content type when sending a FormData object is multipart/form-data not url encoded.
                  Further more the proper boundary must be set for the request, which the user is unable to do. For this XMLHttpRequest sets the correct content type with the required boundary.
                  So all you have to do is not set the content type and it'll work.

                  var xhr=new XMLHttpRequest();
                  xhr.open('post',url,true);
                  //xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");<--don't do this
                  var formData=new FormData(form);
                  formData.append('update', true);    // makes no difference
                  xhr.send(formData);
                  xhr.onload=function() {
                      alert(this.response);
                  };
                  

                  这篇关于XMLHttpRequest &amp;FormData 未提交数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 XMLHttpRequest 通过复制粘贴 javascript 接收 php 图像数据 下一篇:php setcookie 不适用于 ajax 调用

                  相关文章

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

                      <legend id='E8fI1'><style id='E8fI1'><dir id='E8fI1'><q id='E8fI1'></q></dir></style></legend>
                    1. <small id='E8fI1'></small><noframes id='E8fI1'>

                      <tfoot id='E8fI1'></tfoot>

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