加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax异步

发布时间:2020-12-15 21:47:42 所属栏目:百科 来源:网络整理
导读: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
<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">密&nbsp;&nbsp;&nbsp;&nbsp;码:</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="提交">
&nbsp;
<input name="b_reset" type="reset" class="btn_grey" id="b_reset" value="重置"></td>
</tr>
</table>
</form>

</body>




var net=new Object(); //定义一个全局变量net
//编写构造函数
net.AjaxRequest=function(url,onload,onerror,method,params){
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
this.loadDate(url,params);
}
//编写用于初始化XMLHttpRequest对象并指定处理函数,最后发送HTTP请求的方法
net.AjaxRequest.prototype.loadDate=function(url,params){
if (!method){
method="GET";
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}
if (this.req){
try{
var loader=this;
this.req.onreadystatechange=function(){
net.AjaxRequest.onReadyState.call(loader);
}


this.req.open(method,true);
if(method=="POST"){
this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
this.req.send(params);
}catch (err){
this.onerror.call(this);
}
}
}


//重构回调函数
net.AjaxRequest.onReadyState=function(){
var req=this.req;
var ready=req.readyState;
if (ready==4){
if (req.status==200 ){
this.onload.call(this);
}else{
this.onerror.call(this);
}
}
}
//重构默认的错误处理函籹
net.AjaxRequest.prototype.defaultError=function(){
alert("错误数据nn回调状态:"+this.req.readyState+"n状态: "+this.req.status);
}

<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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读