1. 程式人生 > >SpringMVC ajax 參數請求

SpringMVC ajax 參數請求

ajax div tails pojo ice ror class tle mapping

[email protected]

註: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");
                    }
                });

[email protected] 接收對象

註: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的參數和值

參考博客:@[email protected](接收json數據)

詳解,參考博客:@RequestBody, @ResponseBody 註解詳解(轉)

註:JavaScript對象變量對應pojo對象私有變量

SpringMVC ajax 參數請求