日期:2014-05-16  浏览次数:20717 次

ajax验证某个值是否存在
function createXMLHttpRequest(){
			if(window.XMLHttpRequest) { //Mozilla 浏览器 
				XMLHttpReq = new XMLHttpRequest(); 
			} 
			else if (window.ActiveXObject) { // IE浏览器 
				try { 
					XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); 
				} catch (e) { 
					try { 
						XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
					} catch (e) {} 
				}
			}
		}
		
		function handleResponse(){
			if(XMLHttpReq.readyState == 4){
				var xml = XMLHttpReq.responseXML;  //获得的xml数据   
				var flag = xml.getElementsByTagName("message")[0].firstChild.data;  //获得传过来的数据
				if(flag == 1){
					document.getElementById("code").focus();
					document.getElementById("m1").innerHTML = "服务器编码已存在!";
				}else{
					document.getElementById("m1").innerHTML = "服务器编码可用!";
				}
			}
		}
		
		function sendRequest(){
			var code = document.getElementById("code").value;
			var reg=/[^0-9]/g ;
			if(code == ""){
				document.getElementById("code").focus();
				return;
			}
			if(reg.test(code)){
				document.getElementById("code").focus();
				document.getElementById("m1").innerHTML = "只能输入数字!";
			   	return false; 
			}else{
				document.getElementById("m1").innerHTML = "";
			}
			createXMLHttpRequest();
			var url = "server!viewServerCode.action?code=" + code;
			XMLHttpReq.open("get", url, true);
			XMLHttpReq.onreadystatechange = handleResponse;
			XMLHttpReq.send(null);
		}

?

     public String viewServerCode() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();   
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		boolean bool = accountService.getValidateServer(code);
		String str = "";
		str +="<root>";
		if(bool){
			str += "<message>1</message>";
		}else{
			str += "<message>0</message>";
		}
		str += "</root>";
		System.out.println(str);
		out.print(str);
		out.close();
		return null;
	}

?