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

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

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

    1. <tfoot id='Uf6ga'></tfoot>

        <legend id='Uf6ga'><style id='Uf6ga'><dir id='Uf6ga'><q id='Uf6ga'></q></dir></style></legend>

        为什么输入 type=file 不能与 $.ajax 一起使用?

        Why input type=file not working with $.ajax?(为什么输入 type=file 不能与 $.ajax 一起使用?)

            <tbody id='kcQ0V'></tbody>
          <tfoot id='kcQ0V'></tfoot>

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

              <legend id='kcQ0V'><style id='kcQ0V'><dir id='kcQ0V'><q id='kcQ0V'></q></dir></style></legend>

                <bdo id='kcQ0V'></bdo><ul id='kcQ0V'></ul>
                • <i id='kcQ0V'><tr id='kcQ0V'><dt id='kcQ0V'><q id='kcQ0V'><span id='kcQ0V'><b id='kcQ0V'><form id='kcQ0V'><ins id='kcQ0V'></ins><ul id='kcQ0V'></ul><sub id='kcQ0V'></sub></form><legend id='kcQ0V'></legend><bdo id='kcQ0V'><pre id='kcQ0V'><center id='kcQ0V'></center></pre></bdo></b><th id='kcQ0V'></th></span></q></dt></tr></i><div id='kcQ0V'><tfoot id='kcQ0V'></tfoot><dl id='kcQ0V'><fieldset id='kcQ0V'></fieldset></dl></div>
                • 本文介绍了为什么输入 type=file 不能与 $.ajax 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在表单中有一个 <s:file> 标签,它生成一个 HTML <input type="file">.当我通过表单提交(例如提交按钮等)提交表单时,动作方法中的一切正常.但是,当我将代码更改为:

                  I have a <s:file> tag inside the form which generates a HTML <input type="file">. When I submit the form via form submission (e.g. submit button, etc.) everything works fine in the action method. However, when I change my code to:

                  $.ajax({
                      url: "actionClass!actionMethodA.action",
                      type: "POST",
                      error: function(XMLHttpRequest, textStatus, errorThrown) {
                                  alert('Error ' + textStatus);
                                  alert(errorThrown);
                                  alert(XMLHttpRequest.responseText);
                              },
                      data: $(form).serialize(),
                      success: function(data) {
                                  ...
                              }
                  });
                  

                  在后端,file字段总是null.

                  file 字段在 action 类中定义如下(带有 setter 和 getter):

                  The file field is defined in the action class as follow (with setter and getter):

                  private File impFileUrl;
                  

                  是不是因为现在表单被序列化了,导致后端不能再正确设置文件字段?

                  Is it because now the form is serialized so that the file field can no longer be set properly in the backend?

                  推荐答案

                  这是因为 jQuery.serialize() 只序列化输入元素,而不是其中的数据.

                  It is because jQuery.serialize() serializes only input elements, not the data in them.

                  只有成功的控件"被序列化为字符串.不提交按钮值被序列化,因为表单不是使用按钮.对于要包含在序列化的表单元素的值字符串,元素必须有一个名称属性.复选框的值和单选按钮(单选"或复选框"类型的输入)包括在内仅当它们被检查时.来自文件选择元素的数据不是序列化.

                  Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

                  但这并不意味着你不能用ajax上传文件.可能会使用其他功能或插件来发送 FormData 对象.

                  But it doesn't mean that you can't upload files with ajax. Additional features or plugins might be used to send FormData object.

                  如果您设置了正确的选项,您也可以将 FormData 与 jQuery 一起使用:

                  You can also use FormData with jQuery if you set the right options:

                  var fd = new FormData(document.querySelector("form"));
                  fd.append("CustomField", "This is some extra data");
                  $.ajax({
                    url: "actionClass!actionMethodA.action",
                    type: "POST",
                    data: fd,
                    processData: false,  // tell jQuery not to process the data
                    contentType: false   // tell jQuery not to set contentType
                  });
                  

                  这篇关于为什么输入 type=file 不能与 $.ajax 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What are valid deviceNames for Chrome emulation testing with Protractor?(使用 Protractor 进行 Chrome 模拟测试的有效设备名称是什么?)
                  Protractor Check if Element Does Not Exist(量角器检查元素是否不存在)
                  Protractor e2e Tests Login Redirection(Protractor e2e 测试登录重定向)
                  Explain about async/ await in Protractor(解释 Protractor 中的 async/await)
                  Protractor browser.wait doesn#39;t wait(量角器 browser.wait 不等待)
                  How to use Protractor with Angular 2?(如何在 Angular 2 中使用量角器?)

                      <tfoot id='YBaoY'></tfoot>
                        <bdo id='YBaoY'></bdo><ul id='YBaoY'></ul>

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

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