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

IT文库 » Ajax校验是否重复
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址