问题描述
你好我想发布三件事JSON对象和2个文件对象,但请求不会控制器请在下面找到code:
Hi I am trying to post 3 things json object and 2 file object but request is not going to controller pls find below code :
Java脚本funtion:
java script funtion:
<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 code:
Contorller code :
@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");
}
我怀疑与该行的语法问题数据:{jsonString:jsonString,FE:FD,FD1:FD1}
不知道问题是什么,它总是会在错误块。
I doubt the problem with the syntax of the line data: {jsonString:"jsonString", fd:"fd", fd1:"fd1"},
not sure what is the issue it always going in error block.
任何建议
推荐答案
数据:{jsonString:jsonString,FE:FD,FD1:FD1}在这里,你必须删除jsonString参数值加上引号是因为你已经设置好的你的JSON对象字符串化。而添加引号到你的第一个参数。
data: {jsonString:"jsonString", fd:"fd", fd1:"fd1"} in here you have to remove quotes from jsonString parameter value because you have setted your json object stringified. And add quotes to your first parameters.
下面是你更新code。
Here is your updated code.
<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");
}
});
这篇关于我试图做一个AJAX调用后,但请求不会控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!