ajax请求controller

一、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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值