var xmlHttpRequest;
function createXMLHttpRequest() {
var xmlHttp;
if (window.ActiveXobject) {
xmlHttp = new ActiveXobject("MSXML2.XMLHTTP.3.0");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
function validateUserName() {
var username = document.getElementById("tbUserName").value;
var data = "username=" + escape(username);
xmlHttpRequest = createXMLHttpRequest();
xmlHttpRequest.onreadystatechange = callback;
xmlHttpRequest.open("Post", "AjaxPages/CheckUserIsRegister.aspx?num=" + Math.random(), true);
//使用post时需
xmlHttpRequest.setRequestHeader("Content-Length", urlData.length);
xmlHttpRequest.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
xmlHttpRequest.send(data);
}
function callback() {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
var result = xmlHttpRequest.responseText;
if (result == "0") {
document.getElementById("spCheck").innerHTML = "恭喜你,该用户名可以注册";
}
else if (result == "1") {
document.getElementById("spCheck").innerHTML = "对不起,该用户名已使用";
}
else {
document.getElementById("spCheck").innerHTML = result;
}
}
}
}
纯javascript 实现 Ajax 验证用户名是否被占用
最新推荐文章于 2021-04-15 14:55:40 发布