网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/10/10 浏览:3 次 )

直接贴代码!

一、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校验是否重复的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

上一篇:Servlet 与 Ajax 交互一直报status=parsererror的解决办法
下一篇:Spring MVC前端与后端5种ajax交互方法【总结】
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。