
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
span {
border: 1px solid black;
padding: 0 40px;
}
</style>
</head>
<body>
<div id="box">
<input type="text" id="mytxt"><span id="veriEle">2340</span>
<button id="btn">驗證</button>
</div>
<script>
function loadVeri() {
ajax的get傳參
var xhr = null;
try {
xhr = new XMLHttpRequest()
} catch (err) {
xhr = new ActiveXObject("Microsoft.XML")
}
調用open方法
?action=verification
ajax的get傳參 在地址欄傳參 安全性低
數據量大小有限制 不同瀏覽器不同 一般4KB左右
傳參action=verification
xhr.open("get", "http://127.0.0.1:3000/VueHandler/AdminLoginAndRegHandler?action=verification", true)
發送請求
xhr.send()
等待數據返回
xhr.onreadystatechange = function () {
if (xhr.status == 200 && xhr.readyState == 4) {
console.log(xhr.responseText)
var veri = JSON.parse(xhr.responseText)
console.log(veri)
if (veri.success) { 渲染驗證碼
var veriEle = document.getElementById("veriEle");
veriEle.innerHTML = veri.data;
}
}
}
}
loadVeri()
校驗驗證碼
var btn = document.getElementById("btn");
btn.onclick = function () {
var mytxtVal = document.getElementById("mytxt").value;輸入框中的驗證碼
var xhr = null;
try {
xhr = new XMLHttpRequest()
} catch (err) {
xhr = new ActiveXObject("Microsoft.XML")
}
調用open用get方式 把驗證碼傳遞給后台
xhr.open("get", "http://127.0.0.1:3000/VueHandler/AdminLoginAndRegHandler?action=checkVerification&veri=" + mytxtVal, true)
調用send
xhr.send()
等待數據返回
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(JSON.parse(xhr.responseText))
}
}
}
</script>
