Spring mvc +jquery ajax+json的异步实例

时间:2021-06-15 21:33:56

前端部分代码

  $(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();

 }