我在表单中有一个An important fact about Monte Carlo methods is that the estimates for
each state are independent.
标记,用于生成HTML <s:file>
。当我通过表单提交(例如提交按钮等)提交表单时,一切都在动作方法中正常工作。但是,当我将代码更改为:
<input type="file">
在后端,$.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
。
文件字段在操作类中定义如下(使用setter和getter):
null
是不是因为现在表单被序列化了,以至于后端无法再正确设置文件字段?
答案 0 :(得分:2)
这是因为jQuery.serialize()仅序列化输入元素,而不是序列化数据。
仅成功控制&#34;被序列化为字符串。没有提交 由于表单未使用a提交,因此按钮值已序列化 按钮。对于要包含在序列化中的表单元素的值 string,该元素必须具有name属性。复选框中的值 和单选按钮(类型为&#34;输入&#34;或&#34;复选框&#34;的输入)包括在内 只有在他们被检查。 来自文件选择元素的数据不是 序列强>
但这并不意味着您无法使用ajax上传文件。其他功能或插件可能会用于发送FormData
object。
如果设置了正确的选项,也可以将
FormData
与jQuery一起使用: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 });