<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>ASP+AJAX 檢測使用者名稱</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<script type="text/javascript">
function checkUserName(userName) {
var xmlhttp;
// 跨瀏覽器建立 XMLHttpRequest
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e) {
try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e) {
try { xmlhttp = new XMLHttpRequest(); }
catch(e) {}
}
}
if (!xmlhttp) { alert("瀏覽器不支援 AJAX"); return; }
// 發送請求,使用 escape 編碼中文
xmlhttp.open("GET", "checkUserName.asp?userName=" + escape(userName), true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
if (xmlhttp.responseText == "1")
alert("對不起,該使用者名稱已存在!");
else
alert("恭喜!該使用者名稱未被註冊!");
} else {
alert("網路失敗。");
}
}
};
xmlhttp.send(null);
}
</script>
<input type="text" id="username" onblur="checkUserName(this.value)" placeholder="輸入使用者名稱" />
</body>
</html>
參考 ID 66950,回傳 1 存在 / 0 不存在。
encodeURIComponent 取代 escape(已棄用)。Response.ContentType = "application/json"。fetch 或 axios 現代 API(需 Polyfill 支援舊瀏覽器)。