Javascript FormData vs jQuery#serialize(),有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33469684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FormData vs jQuery#serialize(), What is the difference?
提问by L84
Recently I was submitting a form using AJAX.
最近我正在使用 AJAX 提交表单。
In researching the best method I saw some AJAX submissions using jQuery#serialize()and others using FormData. For example.
在研究最佳方法时,我看到一些 AJAX 提交jQuery#serialize()使用FormData. 例如。
One submission did this:
一个提交是这样做的:
data: $('form').serialize()
while the other did:
而另一个则是:
var formData = new FormData($('form')[0]);
data: formData
So what is the difference between FormDataand jQuery#serialize()?
那么FormData和之间有什么区别jQuery#serialize()?
回答by charlietfl
The main difference from a usage standpoint is that you can't serialize files, only file names....the valueof a file input.
从使用角度来看,主要区别在于您不能序列化文件,只能序列化文件名....value文件输入的文件名。
FormDataobject on the other hand also includes files if applicable.
FormData另一方面,如果适用,对象还包括文件。
Also serialize()will work in older browsers that don't support the FormData API for example IE < 10
也serialize()适用于不支持 FormData API 的旧浏览器,例如 IE < 10
reference FormData docs
回答by Christian Petersen
One more important difference is the treatment of empty input-fields in forms.
一个更重要的区别是表单中空输入字段的处理。
serialize()只包含带有值的输入字段。键/值对中将缺少未选中的复选框或空的输入字段。因此,您的应用程序的后端必须进行一些簿记,以便将缺失的键解释为空值。
FormData将为您提供所有表单字段及其值的完整列表。即使它们是空的。

