日期:2014-05-16 浏览次数:20816 次
//创建ajax请求对象
function createXml() {
if( window.XMLHttpRequest ) {
return new XMLHttpRequest();
} else if( window.ActiveXObject ) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
//发送请求
function sendAjax(url, mode) {
var xmlHttp=createXml();
if(xmlHttp) {
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function(){ callback(xmlHttp, mode); };
xmlHttp.send(null);
}
}
//回传方法
function callback(xmlHttp, mode) {
if(xmlHttp.readyState==4) {
if(xmlHttp.status==200) {
showResult(xmlHttp, mode);
}
}
}
//显示检测结果
function showResult(xmlHttp, mode) {
var rtnVal = xmlHttp.responseXML.getElementsByTagName("rtnVal")[0].firstChild.data;
if(mode=="check") {
if(rtnVal=="NAME_EXISTS") {
document.getElementById("txtData").value="该用户名已被注册";
} else if( rtnVal=="NAME_AVAILABLE") {
document.getElementById("txtData").value="";
}
} else if(mode=="fetch") {
salt=rtnVal;
}
}
function doTest() {
var url = "RegisterServlet?name=abcdefg";
document.getElementById("txtData").value="检测中...";
sendAjax(url, "check");
alert(document.getElementById('txtData').value=='');
//setTimeout("alert(document.getElementById('txtData').value=='');",1000);
}