初始化form表单时serialize()和new FormData()的用法区别
1、var formser = new FormData($('#searchForm')[0]);//获取表单的参数
formser.append("test",test)//可增加请求的参数
2、 var formser=$('#dataForm').serialize();
注意:serialize只能序列化表单中的数据 ,比如文本框等input select等的数据,但是对于文件,比如文件上传,无法实现 ,需要使用FormData
例:
<form id="tf" method="post" action="${ctxPath}/mobile/xxx.do" enctype="multipart/form-data">
<input type="file" name="file" id="file1_0" value=""
accept="image/jpg,image/jpeg,image/png,image/gif,image/bmp"
onchange="imgChange('z_photo1','z_file1','file1_0');"/>
</form>
$.ajax({
type: 'POST',
data: uploadFormData,
url: '/Artical/Publist',//TypeError: 'append' called on an object that does not implement interface FormData.
processData: false, //processData 默认为false,当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
contentType: false, //https://segmentfault.com/a/1190000007207128(contentType详解)
//这两个参数如果不写,则会报红色出的错误
async: false,
success: function (data) {
if (typeof (data) == undefined) {
alert("用户信息已丢失,请重新登录!");
window.parent().location.href = "/Account/Login";
}
if (data.ErrorMsg == "") {
alert('美文发布成功!');
} else { alert(data.ErrorMsg); }
}
});