简单的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