最近有个项目要求跨域post来上传文件,但是运行代码以抛出the request was rejected because no multipart boundary was found异常,以下是jsp页面的代码,哪位大神可以告诉我是怎么回事,该如何修改代码?
<%@ page language="java" pageEncoding="UTF-8"%>
<html><head>
<title>Upload</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<script type="text/javascript">
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(function(){
$('.sumbit').click(function(){
var formData = new FormData($('#form1'));
var jsonuserinfo = $('#form1').serializeObject();
alert(JSON.stringify(jsonuserinfo));
alert(formData);
$.ajax({
type: "post", // 或者 "get"
url: "http://localhost:8020/SmartHomeCloudService/DeviceDriver/Upload",
dataType: "json",
data:formData,
contentType:"multipart/form-data",
crossDomain:true,//跨域
processData:false,
success:function(){
alert("success");
},
error:function(){
alert("error");
}
});
});
});
</script>
</head>
<body>
<h3 align="center">jquery Ajax</h3>
<iframe name="upload" style="display:none"></iframe>
<form id="form1" name="form1" method="post" target="upload" action="" encType="multipart/form-data">
<p></p>
file:<input type="file" name="upload_file" id="upload_file" />
<p></p>
<input class="sumbit" type="button" value="提交">
</form>
</body>
</html>