一、get请求
(1)RequestParam
$.ajax({
type : "get",
url : "/admin/user/updateRole/",
async:false,
data: {
userId: userId,
roleId: roleId
},
contentType: "application/json;",
success : function (data) {
alert(data.msg);
window.location.reload(); //刷新页面
}
});
@GetMapping("/user/updateRole")
@ResponseBody
public R updateRole(@RequestParam("userId")Integer userId,@RequestParam("roleId")Integer roleId){
userService.updateUserRole(userId,roleId);
return R.ok("更新成功");
}
(2)PathVariable
function exportWord() {
var selects=[];
var count=0;
$("input[name='items']:checked").each(function () {
if ($(this).prop('checked')){
selects[count++]=$(this).val();
}
});
if (selects.length === 0){
alert("请在表格第一列勾选申报信息表")
} else{
location.href='/admin/table2/export/word/'+selects;
}
}
@RequestMapping("/export/word/{selects}")
public R exportWord(@PathVariable String[] selects, HttpServletRequest request, HttpServletResponse response) throws IOException, TemplateException {
Integer[] appIds = CommonUtils.stringArrayToIntegerArray(selects);
// 一条记录单个word文件
if (appIds.length == 1){
System.out.println(appIds[0]);
tjTalentsService.exportWord(appIds[0],request,response);
}else if (appIds.length > 1){
// 导出成zip文件
tjTalentsService.exportWordZip(appIds,request,response);
}
return R.ok("操作成功!");
}
(3)跳转请求
window.location.href="/apply/table1/New?appSubject="+appSubject+"&appResearch="+Research
@GetMapping("/New")
public ModelAndView table2(@RequestParam("appSubject")String appSubject,@RequestParam("appResearch")String appResearch){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("appSubject",appSubject);
modelAndView.addObject("appResearch",appResearch);
modelAndView.setViewName("/applyTable/table1/table1");
return modelAndView;
}
二、post请求
(1)RequestParam接收
$.ajax({
type : 'POST',
url: "/admin/account/create",
dataType:"json",
data : {
company:company
},
success : function(res) {
alert(res.msg)
}
});
@PostMapping("/create")
@ResponseBody
public R create(@RequestParam("company") String company){
System.out.println(company);
// 查询所有的账号
return R.ok("创建成功,您的账号为");
}
(2)RequestBody接收
//拼装json数据
var surveyResponse = {};
var questionIdList2= new Array();
questionIdList2[0] = 1;
questionIdList2[1] = 2;
questionIdList2[3] = 3;
surveyResponse.surveyTitle = "测试标题";
surveyResponse.surveyIntroduction = "测试标题";
surveyResponse.surveyAuthor = "测试作者";
surveyResponse.questionIdList = questionIdList2;
$.ajax({
type : 'POST',
url: '/admin/survey/save2',
contentType : "application/json;charset=utf-8" ,
dataType:"json",
data : JSON.stringify(surveyResponse),
success : function(data) {
}
});
@RequestMapping("/save2")
@ResponseBody
public String putMessage(@RequestBody SurveyResponse surveyResponse){
return "success";
}
// 实体类
@Data
public class SurveyResponse {
private String surveyTitle;
private String surveyAuthor;
private String surveyIntroduction;
private List<String> questionIdList;
}