加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax校验是否重复的实现代码

发布时间:2020-12-15 23:02:18 所属栏目:百科 来源:网络整理
导读:直接贴代码! 一、controller层: @ResponseBody @RequestMapping("entityFindByCode") public String entityFindByCode(Entity bean,HttpSession httpSession,Model m,HttpServletResponse res) throws IOException{ res.setContentType("text/plain; chars

直接贴代码!

一、controller层:

@ResponseBody 
@RequestMapping("entityFindByCode") 
public String entityFindByCode(Entity bean,HttpSession httpSession,Model m,HttpServletResponse res) throws IOException{ 
  res.setContentType("text/plain; charset=UTF-8"); 
  //res.reset(); 
  //PrintWriter out = res.getWriter(); 
  String data=null; 
  @SuppressWarnings("unchecked") 
  List<Entity> list = (List<Entity>) this.service.find("beanByCode","Entity",new Object[]{bean.getCode()}); 
  if(list.size()==0){ 
    data="YES"; 
  }else{ 
    data="NO"; 
  } 
  res.getOutputStream().write(data.getBytes()); 
  //out.print(data); 
  //out.flush(); 
  //out.close(); 
  return data; 
}

二、html层:

$.ajax({ 
      url    :'entityFindByCode',type   :'post',dataType :'text',data   :{code:$("#code").val()},//{key,value} 
      success  :function(data) { 
        if (data == "YES"){ 
          document.getElementById("msg-code").innerHTML = "(<fmt:message key='warn.field.required.unique' bundle='${bundle }' />)"; 
          return true; 
        } 
        else { 
          $("#div-code").addClass("has-error"); 
          document.getElementsByName("code")[0].value = "";  
          //document.getElementsByName("code")[0].setAttribute("placeholder","<fmt:message key='warn.duplicate' bundle='${bundle }' />");  
          document.getElementById("msg-code").innerHTML = "(<fmt:message key='warn.duplicate' bundle='${bundle }' /> !!!)"; 
          return false; 
        } 
      } 
    }); 

以上所述是小编给大家介绍的Ajax校验是否重复的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读