Spring mvc +jquery ajax+json的異步實例

前端部分代碼

  $(document).ready(function() {
 // 根據身份證號填充表單
 $("#p_Bip_bip_citizenid").blur(function() {
  if (isIdCardNo($("#p_Bip_bip_citizenid").val())) {
   $.ajax({
    type: "POST",
    url: "/gljt.allowance.do",
    data: "m=loadSubtractBipById&idCard=" + $("#p_Bip_bip_citizenid").val(),
    dataType: "json",
    success: function(jsonData) {
     if (jsonData.result=='none') {
      alert("您所錄入的身份證不是高齡津貼的新增人員,無法錄入減少人員數據");
     } else if(jsonData.result=='error'){
      alert("提取信息失敗,請稍後在試。");
      document.getElementById("p_Bip_bip_name").readOnly = false;
      //沒有查到信息則根據身份證將將性別、生日字段讀出並賦值
     } else if(jsonData.result=='empty'){
      document.getElementById("p_Bip_bip_name").readOnly = false;
      $("input[name='p_Bip_bip_sex']").each(function(i,data){if($(data).val()==idcard_getsex($("#p_Bip_bip_citizenid").val())){$(data).attr("checked",true);}});
      if(idcard_getsex($("#p_Bip_bip_citizenid").val())=="1"){
       setFormValue('p_Bip_','sex12','男');
       setFormValue('p_Bip_','bip_sex','1');
      }
      if(idcard_getsex($("#p_Bip_bip_citizenid").val())=="2"){
       setFormValue('p_Bip_','sex12','女');
       setFormValue('p_Bip_','bip_sex','2');
      }
      $("input[name='p_Bip_bip_birthday']").val(idcard_getbirthday($("#p_Bip_bip_citizenid").val()));
     } else{
      document.getElementById("p_Bip_bip_name").readOnly = true;
      //循環JSON數據  自動匹配元素賦值
      for (var one in jsonData) {
       for(var key in jsonData[one]) { 
        var level = document.getElementById("level").value;
        var street_id = document.getElementById("streetId").value;
        if(level == 2) {
         if (key != "unit_id") {
          setFormValue('p_Bip_',key,jsonData[one][key]);
         } else {
          if(key == "unit_id"){
           if(street_id != jsonData[one]["community_parentId"]){
            setFormValue('p_Bip_','unit_id','');
            alert("您所錄入的[老年人口]不屬於本街道,如需錄入請繼續!");
           } else {
            setFormValue('p_Bip_','unit_id',jsonData[one][key]);
           }
          }
         }
        }else if(level == 3){
         if (key != "unit_id") {
          setFormValue('p_Bip_',key,jsonData[one][key]);
         } else {
          if(key == "unit_id") {
           if(street_id != jsonData[one]["unit_id"]){
            setFormValue('p_Bip_','unit_id','');
            alert("您所錄入的[老年人口]不屬於本社區,如需錄入請繼續!");
           } else {
            setFormValue('p_Bip_','unit_id',jsonData[one][key]);
           }
          }
         }
        } else {
         setFormValue('p_Bip_',key,jsonData[one][key]);
        }
        if(key=="bip_sex"){
         if(jsonData[one][key]=="1"){
          setFormValue('p_Bip_','sex12','男');
         }
         if(jsonData[one][key]=="2"){
          setFormValue('p_Bip_','sex12','女');
         }
        }
        //if(key=="lsphoto"){
        // PreviewImagejs('Preview',jsonData[one][key],133,187);
        //}
        if(key=="lsphoto"){
         if(jsonData[one][key]!=null && jsonData[one][key] != ""){
          document.getElementById("pho").style.display = "";
          document.getElementById("Preview").style.display = "none";
          //PreviewImagejs('Preview',jsonData[one][key],133,187);
          var url="/servlet/ImagesServlet?path="+jsonData[one][key];
          var photo=document.getElementById("idImg");
          //photo.setAttribute("src",url);
          photo.src = url;
         }
         else{
          document.getElementById("pho").style.display = "none";
          document.getElementById("Preview").style.display = "";
          
         }
        }
       }
      }
     }
    }
   });
  }
 });
});

 

後端部分代碼:

  public void loadBipByIdCard(HttpServletRequest request,
   HttpServletResponse response) {
  response.setCharacterEncoding("utf-8");
  PrintWriter pw = null;
  try {
   pw = response.getWriter();
   Bip bip = new Bip();
   String bip_citizenid = ServletRequestUtils.getStringParameter(
     request, "idCard", "");
   bip.setBip_citizenid(bip_citizenid);
   List<Bip> bips = this.bipService.queryByVO(bip);
   
   bip = bips.size() < 1 ? null : bips.get(0);
   if (null == bip) {
    pw.println("{\"result\":\"empty\"}");
   } else {
    if(bip.getUnit_id() != null && bip.getUnit_id() != ""){
     bip.setStreet_id(MirrorCacheTool.getProp("unit", bip.getUnit_id(), "parentid").toString());
    }
    JSONArray json = JSONArray.fromObject(bip, JSONDataFormat
      .setDataFormat());
    pw.println(json);
   }

  } catch (Exception e) {
   pw.println("{\"result\":\"error\"}");
   e.printStackTrace();
  }
  pw.flush();
  pw.close();

 }

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章