ajax操作ajax异步获取数据,查询用户数据...
时间:2010-08-12 来源:lianjianguo888
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function CheckUserName()
{
var us=document.getElementById("txtname").value;
if(us!="")
{
createXMLHttpRequest();
var url= "RegistValidate.ashx?username="+escape(document.getElementById("txtname").value);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=ShowResult;
xmlHttp.send(null);
}
}
function ShowResult()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var s;
s=xmlHttp.responseText;
alert(s);
}
}
}
</script>
textbox1.Attributes.Add("onblur","CheckUserName()");
————————————————————————————————————————————————————————
第二种方法:
___________________________________________________________________________________________
JScript code//创建异步对象 function createXMLHttpRequest1() { if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function checkUserName(userName) { if(userName!="" && userName.length>5) { createXMLHttpRequest1(); var queryString = "exeistsUserName.aspx?"; //在 CheckfirstName 检测用户名 queryString = queryString + "UserName="+userName; //方法 handleStateChange1 没有参数 直接使用方法名字引用 xmlHttp.onreadystatechange = handleStateChange1; //如果 有参数就是 下面的例子: //xmlHttp.onreadystatechange = function(){ handleStateChange1(参数)} xmlHttp.open("GET", queryString, true); xmlHttp.send(null); } } function handleStateChange1() { if(xmlHttp.readyState == 4) { if(xmlHttp.status == 200) { var data =xmlHttp.responseText; var div_uNameMsg = document.getElementById("div_uNameMsg"); div_uNameMsg.style.display=""; if(data=="1") { div_uNameMsg.style.backgroundColor="#E45142"; div_uNameMsg.innerHTML="对不起该用户已存在!"; isExeistsUName=true; } else if(data=="0") { div_uNameMsg.style.backgroundColor="#008000"; div_uNameMsg.innerHTML="该用户可以使用!"; isExeistsUName=false; } else if(data=="-1") { div_uNameMsg.style.backgroundColor="#E45142"; div_uNameMsg.innerHTML="服务器繁忙,出现未知错误!"; isExeistsUName=true; } } } }
用户名:<asp:TextBox ID="txtName" runat="server" onblur="checkUserName(this.value)"></asp:TextBox>