小编给大家分享一下form表单数据如何封装成json格式并提交给服务器,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
目前创新互联已为上千的企业提供了网站建设、域名、网站空间、绵阳服务器托管、企业网站设计、旅顺口网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
1、jsp代码,form表单:
2、js代码:
$(".fee_sure").click(function () { var dataObj = $("#costForm").serialize(); var payType = $('input[name="payType"]:checked').val(); if(payType == undefined){ alert("请选择付款类型"); return ; }else{ $.ajax({ url:"/webCenter.do?method=saveUserMessage", type: "post", dataType:"json", data:dataObj, cache:false, ifModified:true, success:function(json){ if(json.flag == 1){ alert("1234"); } } }) } })
3、后台action处理:
@RequestMapping(params ="method=saveUserMessage",method=RequestMethod.POST) //导入发言 public void saveUserMessage(int userId,HttpServletRequest request,HttpServletResponse response){ try { HttpSession session = this.getSession(request); Adminuser adminUser = session.getAttribute("centerAdminUser") == null?null:(Adminuser) session.getAttribute("centerAdminUser"); if(adminUser == null){ try { response.sendRedirect(request.getContextPath()+"/center/index.jsp"); } catch (Exception e) { e.printStackTrace(); } }else{ String billNumber = request.getParameter("billCode")==null?"":request.getParameter("billCode"); String isBaodao = request.getParameter("baodao")==null?"0":request.getParameter("baodao"); String haspay = request.getParameter("pay")==null?"0":request.getParameter("pay"); String payType = request.getParameter("payType")==null?"0":request.getParameter("payType"); String isFkpz = request.getParameter("isReceiveProof")==null?"0":request.getParameter("isReceiveProof"); String isYkfp = request.getParameter("isBill")==null?"0":request.getParameter("isBill"); String isYlfp = request.getParameter("isDrawBill")==null?"0":request.getParameter("isDrawBill"); JSONObject jsonObject = new JSONObject(); //AdminuserConferences adminuserConferences = webService.getAdminuserConferences(adminUser.getAdminuserId()); UserInfo userInfo = webService.getUserInfoById(userId); if(userInfo!=null) { jsonObject.accumulate("flag",1); ChcRegUser chcRegUser = webService.getChcRegUser(userId); if(chcRegUser!=null) { chcRegUser.setHasPay(Integer.parseInt(haspay)); chcRegUser.setPayType(Integer.parseInt(payType)); if(chcRegUser.getHasPay()-chcRegUser.getPay()==0) { chcRegUser.setStateType(1); } webService.saveObject(chcRegUser); userInfo.setIsBaodao(Integer.parseInt(isBaodao)); userInfo.setIsFkpz(Integer.parseInt(isFkpz)); userInfo.setBillNumber(billNumber); userInfo.setIsYlfp(Integer.parseInt(isYlfp)); userInfo.setIsYkfp(Integer.parseInt(isYkfp)); webService.saveObject(userInfo); jsonObject.accumulate("pay", chcRegUser.getPay()); jsonObject.accumulate("haspay", chcRegUser.getHasPay()); } }else { jsonObject.accumulate("flag",0); } writeJson(response, jsonObject.toString()); } } catch (Exception e) { e.printStackTrace(); } }
以上是“form表单数据如何封装成json格式并提交给服务器”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!