Home  >  Article  >  Web Front-end  >  Ajax check whether data is duplicated

Ajax check whether data is duplicated

php中世界最好的语言
php中世界最好的语言Original
2018-04-02 13:43:091679browse

This time I will bring you whether the Ajax verification data is duplicated and whether the Ajax verification data is duplicated. What are the precautions?The following is a practical case, let's take a look.

1. Controller layer:

@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 list = (List) 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; 
}

2. HTML layer:

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

I believe you have mastered it after reading the case in this article For more exciting methods, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use ajax to realize pop-up login

Ajax steps to delete data and view data operations Detailed explanation

The above is the detailed content of Ajax check whether data is duplicated. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn