//1.一个ajax
$.ajax({
url:"/",
data:obj,
type:"post",
success:function(data){},//aaa为服务器所返回的值
error:function(){},//服务器连接失败
});
//2.后台接收String[]数组实例
$.ajax({
url:'/',
type:'post',
traditional: true,//配置用数组接收
data:{prjIds:[1,2,3]},//ID数组
success:function(data){},
error:function(error){}
});
@ResponseBody
@RequestMapping("/deleteXmgl")
public R deletexmgl(@RequestParam String[] prjIds) {}//直接使用数组接收
//3.后台接收复杂的对象套对象的bean 使用@requsetbody
var obj = {"taskDefKey": "gwyxtrxmtz_province_task_id","taskList": arr};//json中的key与成员变量要对应
$.ajax({
url:'/project/Gwyxtrxmtz/taskApprove',
type:'post',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(obj),//将我们复杂的obj直接转成json字符串
success:function(data){},
error:function(error){}
});
@ResponseBody
@PostMapping(value = "/taskApprove")
public R taskApprove(@RequestBody ToDoTaskVo toDoTaskVo) {}//使用@RequestBody,我们自定义的bean接收
//4同时接收上传文件和bean对象
var uploadFormData = new FormData($('#submitForm')[0]);//FormData待研究
uploadFormData.append("prjId",Id);
$.ajax({
url: "/project/Gwyxtrxmtz/submit",
type: "post",
data: uploadFormData,
processData: false,
contentType: false,
async: false,
success: function(data) {},
error: function(error){}
});
@ResponseBody
@RequestMapping("/submit")
public R submits(HttpServletRequest request,@RequestParam("file") MultipartFile file,ToDoTaskVo toDoTaskVo) {}
//4.2使用form表单提交接收文件
<form id="toExcleGwyxtrxmForm" method="POST" action="/project/Gwyxtrxmtz/uploadExcle" enctype="multipart/form-data">
<input type="file" id="exampleInputFile" name="file">