//FormData是html5的接口,使用它一行代码便可以拿到整个form表单对象:
var form = new FormData(document.getElementById("form"));
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-1.9.1.min.js"></script>
<meta charset="utf-8" />
<title>Ajax提交form</title>
<script type="text/javascript">
function test(){
var form = new FormData(document.getElementById("form"));
$.ajax({
url:"接口地址",
type:"post",
data:form,
cache: false,
processData: false,
contentType: false,
success:function(data){
alert("操作成功!");
},
error:function(e){
alert("网络错误,请重试!!");
}
});
}
</script>
</head>
<body>
<form id="form" enctype="multipart/form-data">
<input type="text" id="endTime" name="endTime" />
<input type="text" id="type" name="type" />
<input type="file" id="fileAttach" name="fileAttach" />
<input type="button" onclick="test()" value="上传" />
</form>
</body>
</html>
原文(http://blog.csdn.net/qq_33556185/article/details/51086114)