ajax---简单使用Demo

前端之家收集整理的这篇文章主要介绍了ajax---简单使用Demo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

简单的ajax 使用demo。

前台文本框失去焦点(还可以是其他事件)就后台执行查询

同时还会回馈给前台

<script type="text/javascript">
	function Createregxml() {
		try {
			regxml = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			try {

				regxml = new ActiveXObject("Msxm12.XMLHTTP");
			} catch (E) {
				regxml = false;
			}
		}

		if (!regxml && typeof XMLHttpRequest != 'undefined') {
			regxml = new XMLHttpRequest();
		}
		return regxml;
	}

	function returnI() {

		if (regxml.readyState == 4) {
			var Data = regxml.responseText;
			// alert(Data);
			var str = Data.substring(0,3);
			//alert(str);
			if (str == "yes") {
				var divobj = document.getElementById("divusername");
				divobj.innerHTML = "<font color=greed>√</font>可以注册检查通过。";
			} else {
				var divobj = document.getElementById("divusername");
				divobj.style.display = "block";
				divobj.innerHTML = "<font color=red>×</font>这个用户名已经被注册了,请换一个";
			}
		}
	}

	function chcek() {
		var username = document.getElementById("username").value;
		alert(username);
		regxml = Createregxml();
		regxml.onreadystatechange = returnI;
		var url = "service.jsp?";
		regxml.open("post",url + "username=" + username);
		regxml.send(null);
	}
</script>


<body>
	<input type="text" name="username" id="username" onblur="chcek()">
	<div id="divusername"></div>
</body>


service.jsp

后台执行查询操作

<%
	String username = request.getParameter("username");
	System.out.println(username);
	out.clear();
	if ("chen".equals(username)) {
		out.print("no");
	} else {
		out.print("yes");
	}
%>
原文链接:https://www.f2er.com/ajax/166501.html

猜你在找的Ajax相关文章