Home > Web Front-end > JS Tutorial > body text

Ajax check whether duplicate implementation code

亚连
Release: 2018-05-22 16:15:17
Original
1824 people have browsed it

This article mainly introduces the implementation code of Ajax verification for duplication. It is very good and has reference value. Friends in need can refer to

and post the code directly!

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<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; 
}
Copy after login

2. HTML layer:

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

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of the steps to implement the blog post adding category function with PHP AJAX

How to implement the voting function with PHP AJAX

How does PHP get the headers (case) in ajax

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!