form 폼 데 이 터 를 json 형식 으로 패키지 하여 서버 에 제출 하 는 실현 방법

1.jsp 코드,form 폼:

<form action="#" id="costForm">
<input type="hidden" id="userId" name="userId"/>
<div id="COST">
 <img src='<%=path %>/cvc/center/images/close.png' class="close" style="width:34px;position:absolute;right:0px;top:0px;"></img>
 <h2 style="font-weight: bolder;font-size: 14px;">            </h2>
 <ul>
 <li>
  <label for="report">
         :
  </label>
  <input type="radio" id="baodao1" name="baodao" value="1"/> 
  <input type="radio" id="baodao0" name="baodao" value="0"/> 
 </li>
 <li>
  <label for="name">
         :
  </label>
  <span id="costName"></span>
 </li>
 <li>
  <label for="type">
         :
  </label>
  <span id="costType"></span>
 </li>
 <li>
  <label for="sure_fee">
           :
  </label>
  <span id="costType">0</span>
 </li>
 <li>
  <label for="sure_fee">
          :
  </label>
  <span id="price"></span>
 </li>
 <li>
  <label for="already_fee">
          :
  </label>
  <input type="text" id="pay_" name="pay"/>
 </li>
 <li>
  <label for="owe">
         :
  </label>
  <span id="qianPay"></span>
 </li>
 <li>
  <label for="fee_type">
         :
  </label>
  <input type="radio" id="payType1" name="payType" value="3"/>  
  <input type="radio" id="payType2" name="payType" value="4"/>  
  <input type="radio" id="payType3" name="payType" value="5"/>  
  <input type="radio" id="payType4" name="payType" value="2"/>    
 </li>
 <li>
  <label for="fee_evidence">
             :
  </label>
  <input type="radio" id="isReceiveProof1" name="isReceiveProof" value="1"/> 
  <input type="radio" id="isReceiveProof0" name="isReceiveProof" value="0"/> 
 </li>
 <li>
  <label for="fa_piao">
         :
  </label>
  <span id="billTitle"></span>
 </li>
 
  <li>
  <label for="fa_piao">
         :
  </label>
   <input type="text" id="billCode" name="billCode"/>
 </li>
 <li>
  <label for="check_evidence">
         :
  </label>
  <input type="radio" id="isBill1" name="isBill" value="1"/> 
  <input type="radio" id="isBill0" name="isBill" value="0"/> 
 </li>
 <li>
  <label for="get_evidence">
         :
  </label>
  <input type="radio" id="isDrawBill1" name="isDrawBill" value="1"/> 
  <input type="radio" id="isDrawBill0" name="isDrawBill" value="0"/> 
 </li>
 <li>
 
  <label for="date">
         :
  </label>
  <span id="confirmDate"></span>
  <!-- <input type="text" id="confirmDate" name="confirmDate"/> --> 
  <!-- <input id="confirmDate" name="confirmDate" class="text" type="text" onClick="WdatePicker()"/> -->
 </li>
 <li>
  <label for="check_letter">       :</label>
  <span id="isConfirmInvitation"></span>
 </li>
  <li>
  <label for="fa_piao">      :</label>
  <span id="payString"></span>
 </li>
 </ul>
 <div class="fee_opr">
  <button type="button" class="fee_sure" style="border: none;background: #003260;color: #ffffff;padding: 3px 30px;margin-left: 10px;border-radius: 0px;">  </button>
  <button type="button" class="fee_cancel" style="border: none;background: #003260;color: #ffffff;padding: 3px 30px;margin-left: 10px;border-radius: 0px;">  </button>
 </div>
</div>
</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 형식 으로 봉 하여 서버 에 제출 하 는 실현 방법 은 바로 소 편 이 여러분 에 게 공유 하 는 모든 내용 입 니 다.참고 하 시기 바 랍 니 다.여러분 의 많은 응원 바 랍 니 다.

좋은 웹페이지 즐겨찾기