日期:2014-05-17 浏览次数:20839 次
<script type="text/javascript"> var xmlHttpReq = false; //创建XMLHttpRequest对象 function createXMLHttpRequest() { if (window.XMLHttpRequest) {//非IE浏览器 XMLHttpReq = new XMLHttpRequest(); } else if (window.ActiveXobject) {//IE浏览器 try { XMLHttpReq = new ActiveXobject("Maxml2.XMLHTTP"); } catch (e) { try { XMLHttpReq = new ActiveXobject("Microsoft.XMLHTTP"); } catch (e) { } } } } function sendRequest(url) { createXMLHttpRequest(); XMLHttpReq.open("GET", "url", true); XMLHttpReq.onreadyStatechange = processResponse;//指定响应函数 XMLHttpReq.send(null); } function processResponse() { if (XMLHttpReq.readyState == 4) { if (XMLHttpReq.status == 200) { var res = XMLHttpReq.responseXML.getElementsByTagName("res")[0].firstChild.data; alert(res); } else { alert("您所请求的页面有异常"); } } } function userCheck() { var username = document.myform.username.value; var password = document.myform.password.value; if (username == "") { alert("用户名不能为空!"); document.myform.username.focus(); return false; } else { sendRequest('login?username=' + username + '&password=' + password); } } </script> <body vlink="#006666" link="#003366" bgcolor="#e0f0f8"> <center> <h1>AJAX用户登录</h1> <hr> <form action="LoginServlet" method="post" name="myform"> 用户名:<input type="text" name="username"> <p> 密码:<input type="password" name="password"> <p> <hr> <input type="submit" value="提交" onclick="userCheck();"> <input type="reset" value="重置"> </form> </center> </body>
function userCheck() { var username = document.myform.username.value; var password = document.myform.password.value; if (username == "") { alert("用户名不能为空!"); document.myform.username.focus(); return false; } else { sendRequest('login?username=' + username + '&password=' + password); } return true ;<----------------+上 }