ajax异步
<script language="javascript"src="js/jquery-1.11.1.js"></script>
//ajax异步<!-- <script language="javascript"> function checkUser(userName){ if(userName.value==""){ alert("请输入用户名");userName.focus();return; }else{ createRequest('checkuser.jsp?user='+userName.value); } } function createRequest(url){ http_request = false; if(window.XMLHttpRequest){ http_request = new XMLHttpRequest(); }else if(window.ActiveXObject){ try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); //创建XMLHttpRequest对象 } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); //创建XMLHttpRequest对象 } catch (e) {} } } if(!http_request){ alert("不能创建XMLHttpRequest对象"); return false; } http_request.onreadystatechange = getResult; http_request.open('post',url,true); http_request.send(null); } function getResult(){ if(http_request.readyState ==4){ if(http_request.status ==200){ alert(http_request.responseText); } else{ alert("你请求的页面有错误"); } } } </script>--> <script type="text/javascript">//jquery实现 $(document).ready(function(){ $("#b_checkUser").click(function(){ if($("#username").val()==""){ alert("用户名不能为空");$("#username").focus(); }else{ $.get("checkuser.jsp",{user:$("#username").val()},function(data){alert(data);}); } }) }) </script> </head> <body> <tr> <form name="form1" method="post" action=""> <table width="402" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF" bordercolorlight="#FFFFFF" bordercolordark="#777777" bgcolor="#999999"> <tr> <td height="30" colspan="2" bgcolor="#EEEEEE">·用户注册</td> </tr> <tr> <td width="113" height="30" align="center" bgcolor="#FFFFFF">用 户 名:</td> <td width="387" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="30"> <input name="b_checkUser" type="button" class="btn_grey" id="b_checkUser" value="检测用户名" onClick="checkUser(this.form.username);"></td> </tr> <tr> <td height="30" align="center" bgcolor="#FFFFFF">密 码:</td> <td bgcolor="#FFFFFF"><input name="pwd" type="password" id="pwd" size="30"></td> </tr> <tr> <td height="30" align="center" bgcolor="#FFFFFF">确认密码:</td> <td bgcolor="#FFFFFF"><input name="pwd1" type="password" id="pwd1" size="30"></td> </tr> <tr> <td height="40" colspan="2" align="center" bgcolor="#FFFFFF"><input name="b_submit" type="submit" class="btn_grey" id="b_submit" value="提交"> <input name="b_reset" type="reset" class="btn_grey" id="b_reset" value="重置"></td> </tr> </table> </form> </body>
var net=new Object(); //定义一个全局变量net <script> function getTime(){ var loader = new net.AjaxRequest("dealtime.jsp?nocache="+new Date().getTime(),deal,"GET"); } //这个不是给后台的,而是给浏览器的,值是一个时间戳,这就保证了这个url总是以前没访问过的url,防止浏览器发现url在缓存中存在就直接从缓存读取了而不是从服务器获取。 function deal(){ document.getElementById("clock").innerHTML="当前时间为:"+this.req.responseText; } function onerror(){ alert("出错了") } window.setInterval("getTime();",1000); </script> </head> <body onload="getTime()"> <div id="clock">时间</div> </body> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |