标签:
ajaxSubmit提交form表单
依赖脚本:
1 <script src="<%=request.getContextPath()%>/huangdao/jquery/jquery-form.js" type="text/javascript"></script>
HTML
1 <form id="saveForm" name="form01" class="demoform"> 2 <input id = "" name="" class="" value=""/> 3 <input type="button" class="btn baocun" value="保存" onclick="savePerosnalInfo();"> 4 </form>
JS
1 <script type="text/javascript"> 2 function savePerosnalInfo(){ 3 4 var options = { 5 6 url : "<%=request.getContextPath()%>/savePersonalInfo.action?id=${info.cardid}", 7 type : "post", 8 dataType : "json", 9 success : function(data){//回调函数 10 //alert(data); 11 if(data.path=="success"){//返回信息等于success则提示保存成功 12 alert(" save success!"); 13 //保存成功之后关闭当前页面 14 //window.close(); 15 //$("#abb").hide(); 16 //$(".layer-box").hide(); 17 //隐藏页面(这个页面是由父页面控制的,需要找到父页面的class) 18 $(".layer-box" , parent.document).hide(); 19 //在实现关闭保存页面的同时发现右侧的滚动条消失了,然后添加了下面一句:删除隐藏标签 20 $("body", parent.document).removeClass("overhide"); 21 }else{ 22 alert(" save failed!"); 23 } 24 } 25 }; 26 $("#saveForm").ajaxSubmit(options); 27 28 } 29 </script>
JAVA
1 @RequestMapping(value = "/savePersonalInfo") 2 public void updatePersonalInfo(HdPersonalInfo info, HttpServletRequest request, 3 HttpServletResponse response, ModelAndView model) { 4 5 String cardid = request.getParameter("id"); 6 info.setCardid(cardid); 7 hdPersonalinfoService.updatePersonalInfoByCardid(info); 8 9 10 String path = "success"; 11 //JSON对象 12 /*方式一*/ 13 JSONObject jo = new JSONObject(); 14 jo.put("path",path); 15 //1:对响应设置类型 JSON 16 response.setContentType("application/json;charset=UTF-8"); 17 //2:在响应中写入JSON格式的字符串 18 try { 19 response.getWriter().write(jo.toString()); 20 } catch (IOException e) { 21 22 e.printStackTrace(); 23 } 24 25 26 /*方式二*/ 27 HashMap<String, String> map = new HashMap<String, String>(); 28 map.put("path", path); 29 net.sf.json.JSONObject testJson = net.sf.json.JSONObject.fromObject(map); 30 //1:对响应设置类型 JSON 31 response.setContentType("application/json;charset=UTF-8"); 32 //2:在响应中写入JSON格式的字符串 33 34 try { 35 response.getWriter().write(testJson .toString()); 36 } catch (IOException e) { 37 38 e.printStackTrace(); 39 } 40 41 42 }
标签:
原文地址:http://www.cnblogs.com/zhang13/p/5776177.html