SpringMVC ajax 参数请求

Posted 北冥冰皇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC ajax 参数请求相关的知识,希望对你有一定的参考价值。

1、采用@RequestParam或Request对象获取参数的方法

注:contentType必须指定为:application/x-www-form-urlencoded

    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public HttpResult insert(HttpServletRequest request) {
//        @RequestParam("LoginName") String LoginName
        HttpResult result = new HttpResult();
        Users users = new Users();
        users.setUserId(4);
        users.setRoleId(Integer.parseInt(request.getParameter("RoleId")));
        users.setLoginName(request.getParameter("LoginName"));
        users.setLoginPassword("123456");
        users.setUserName(request.getParameter("UserName"));
        users.setMobilePhone(request.getParameter("MobilePhone"));
        users.setEmail(request.getParameter("Email"));
        users.setIsActive("0");
        users.setOrgId(Integer.parseInt(request.getParameter("OrgId")));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        users.setCreateTime(df.parse(df.format(new Date()),new ParsePosition(0)));

        int i = this.userService.insertSelective(users);
        if(i>0){
            result.setState(ResultType.success.toString());
            result.setMessage("数据插入成功!");
        }else{
            result.setState(ResultType.error.toString());
            result.setMessage("数据插入失败!");
        }
        return result;
    }

ajax代码

                $.ajax({
                    url: \'../user/insert\',
                    type: \'POST\',
//                     data: JSON.stringify({
//                              "loginName": $(\'#inputLoginName\').val(),
//                              "userName": $(\'#inputUserName\').val(),
//                              "mobilePhone": $(\'#inputMobilePhone\').val(),
//                              "email": $(\'#inputEmail\').val(),
//                              "roleId": $(\'#selectRole\').val(),
//                              "orgId": $(\'#selectOrg\').val()
//                      }),  
                     data: {
                         LoginName: $(\'#inputLoginName\').val(),
                         UserName: $(\'#inputUserName\').val(),
                         MobilePhone: $(\'#inputMobilePhone\').val(),
                         Email: $(\'#inputEmail\').val(),
                         RoleId: $(\'#selectRole\').val(),
                         OrgId: $(\'#selectOrg\').val()
                     },
                     contentType: \'application/x-www-form-urlencoded\',
//                     contentType: \'application/json\',
                    dataType: "json",
                    success: function (result) {
                        alert(result.message);
                        $("#myModal").modal("hide");
                        oTable.fnDraw();
                    },
                    error: function (err) {
                        alert("error");
                    }
                });

2、@RequestBody 接收对象

注:contentType为application/json

    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public HttpResult insert(@RequestBody Users users) {
        HttpResult result = new HttpResult();

        int i = this.userService.insertSelective(users);
        if(i>0){
            result.setState(ResultType.success.toString());
            result.setMessage("数据插入成功!");
        }else{
            result.setState(ResultType.error.toString());
            result.setMessage("数据插入失败!");
        }
        return result;
    }

ajax请求

                $.ajax({
                    url: \'../user/insert\',
                    type: \'POST\',
                     data: JSON.stringify({
                              "loginName": $(\'#inputLoginName\').val(),
                              "userName": $(\'#inputUserName\').val(),
                              "mobilePhone": $(\'#inputMobilePhone\').val(),
                              "email": $(\'#inputEmail\').val(),
                              "roleId": $(\'#selectRole\').val(),
                              "orgId": $(\'#selectOrg\').val()
                      }),  
                    contentType: \'application/json\',
                    dataType: "json",
                    success: function (result) {
                        alert(result.message);
                        $("#myModal").modal("hide");
                        oTable.fnDraw();
                    },
                    error: function (err) {
                        alert("error");
                    }
                });

参考博客: SpringMVC Ajax 获取参数的方法

参考博客:Spring MVC无法获取ajax POST的参数和值

参考博客:@RequestBody和@ResponseBody的简单使用接收JSON(接收json数据)

详解,参考博客:@RequestBody, @ResponseBody 注解详解(转)

注:JavaScript对象变量对应pojo对象私有变量

以上是关于SpringMVC ajax 参数请求的主要内容,如果未能解决你的问题,请参考以下文章

ajax请求传参数复杂对象list,后端springmvc接收参数

ajax和springmvc的请求响应原理

Spring注解处理Ajax请求-JSON格式[系统架构:Spring+SpringMVC+MyBatis+MySql]

Springmvc: Ajax

springMVC通过ajax传递参数list对象或传递数组对象到后台

SpringMVC进行Ajax请求页面显示乱码