SpringMVC ajax 參數請求

來源:互聯網
上載者:User

標籤:value   turn   div   error   接收   inpu   ror   tle   lin   

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 參數請求

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.