日期:2014-05-16 浏览次数:20965 次
<html> <body> <script type="text/javascript"> function ajaxFunction(){ var xmlHttp; try{ // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e){ // Internet Explorer try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("您的浏览器不支持AJAX!"); return false; } } } //以上是创建一个XMLHttpRequest对象 //下面这个function是回调函数 xmlHttp.onreadystatechange=function(){ //readyState为4表示服务器处理完成 if(xmlHttp.readyState==4){ //通过DOM修改页面的显示 //返回的数据存在responseText中 document.myForm.time.value=xmlHttp.responseText; } } //建立请求 xmlHttp.open("GET","time.asp",true); //发送请求 xmlHttp.send(null); } </script> <form name="myForm"> //触发ajax 用户: <input type="text" name="username" onkeyup="ajaxFunction();" /> 时间: <input type="text" name="time" /> </form> </body> </html>
<% response.expires=-1 response.write(time) %>
$.ajax({ type: "GET", url: rootURI + "/login", data:{'username' : username, 'password' : password}, complete: checkLoginStatus });
public void login() { if (result.isSuccess()) { ... } else { putJSON("user_not_exist_or_incorrect_password"); } }
putJSON(String data){ ServletActionContext.getResponse().setCharacterEncoding("utf-8"); ServletActionContext.getResponse().setContentType("application/json;charset=UTF-8"); PrintWriter out = ActionUtil.getResponse().getWriter(); out.print(data); }
function checkLoginStatus(data){ if (data.status != 200) { alert('登陆出错,请稍后再试'); return; } if (data.responseText == "incorrect_code") { alert('验证码错误'); } else if (data.responseText == "user_not_exist_or_incorrect_password") { alert('无此用户名或密码错误'); } else { location.href = rootURI + '/admin/index.jsp'; } }