PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

Ajax校验数据有没有重复

php中世界最好的语言
php中世界最好的语言 原创
2018-04-02 13:43:09 1499浏览

这次给大家带来Ajax校验数据有没有重复,Ajax校验数据有没有重复的注意事项有哪些,下面就是实战案例,一起来看一下。

一、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 { 
          $("#p-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; 
        } 
      } 
    });

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

怎样用ajax实现弹窗登录

Ajax删除数据与查看数据操作的步奏详解

以上就是Ajax校验数据有没有重复的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。