您好我正在尝试发布3个json对象和2个文件对象,但请求不会转到控制器请找到下面的代码:
java脚本功能:
<script type="text/javascript">
function submitAllDetails(){
var jsonObj=[{
name:name,
age:age,
rollno:rollno,
add:add,
}];
var jsonString=JSON.stringify(jsonObj);
alert(jsonString);
var fileInput=document.getElementById("Uploadfile");
var file=fileInput.files[0];
var fd = new FormData();
fd.append("fileUpload",file);
var fileInput1=document.getElementById("Uploadfile2");
var file1=fileInput1.files[0];
var fd1 = new FormData();
fd1.append("fileUploadnew",file1);
$.ajax({
url:contextPath +"/submitAllInfo",
type:"POST",
contentType: "application/json; charset=utf-8",
data: {jsonString:"jsonString", fd:"fd", fd1:"fd1"},
async: false,
cache: false,
processData:false,
success: function(response){
alert("in success***");
},
error: function(){
alert("error has occured");
}
});
}
</script>
Contorller代码:
@RequestMapping(value = "/submitAllInfo", method = RequestMethod.POST)
public @ResponseBody ModelAndView insertAllStepDetails(@RequestParam("jsonString") String jsonString,@RequestParam("fd") CommonsMultipartFile[] fileUpload,@RequestParam("fd1") CommonsMultipartFile[] fileUploadnew){
System.out.println("in submit controller !!!");
return new ModelAndView("success");
}
我怀疑行data: {jsonString:"jsonString", fd:"fd", fd1:"fd1"},
的语法问题不确定它在错误块中总是会出现什么问题。
任何建议
答案 0 :(得分:0)
数据:{jsonString:&#34; jsonString&#34;,fd:&#34; fd&#34;,fd1:&#34; fd1&#34;}在这里你必须从jsonString参数值中删除引号因为你已经设置了你的json对象字符串。并在第一个参数中添加引号。
这是您的更新代码。
<script type="text/javascript">
function submitAllDetails(){
// Why usint [] brackets? not needed..
var jsonObj={
name:name,
age:age,
rollno:rollno,
add:add,
};
var jsonString=JSON.stringify(jsonObj);
alert(jsonString);
var fileInput=document.getElementById("Uploadfile");
var file=fileInput.files[0];
var fd = new FormData();
fd.append("fileUpload",file);
var fileInput1=document.getElementById("Uploadfile2");
var file1=fileInput1.files[0];
var fd1 = new FormData();
fd1.append("fileUploadnew",file1);
$.ajax({
url:contextPath +"/submitAllInfo",
type:"POST",
contentType: "application/json; charset=utf-8",
data: {"jsonString":jsonString, "fd":fd, "fd1":fd1}, // you have setted json in jsonString varible
async: false,
cache: false,
processData:false,
success: function(response){
alert("in success***");
},
error: function(){
alert("error has occured");
}
});
答案 1 :(得分:0)
您可以尝试以下代码:
var file_data = $("#UploadedFile").prop("files")[0];
var form_data = new FormData();
form_data.append("file", file_data)
form_data.append("user_id", 123)