ajax请求controller
一、get请求$.ajax({type : "get",url : "/admin/user/updateRole/",async:false,data: {userId: userId,roleId: roleId},contentType: "application/json;",success : function (data) {alert(data.m
·
一、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;
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)