function pwdTest() {
// 1.创建httpXml请求对象
var xmlHttp;
if (window.XMLHttpRequest) //判断浏览器类型
xmlHttp = new XMLHttpRequest();
else if (window.ActiveXObject)
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
// 2.打开链接
var pwd = document.getElementById("pwd").value;//获取标签中属性的值
xmlHttp.open("post", "/testupwd", true)//post请求
// 3.指定回调函数
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {//判断AJAX的状态码
var responseTest = xmlHttp.responseText;// 接收后台返回的参数
document.getElementById("nameMsg").innerText=responseTest;// 将返回的参数显示在页面
window.alert("11111")
}
}
// 4.发送数据
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");// 增加time随机参数,防⽌读取缓存
xmlHttp.send("upwd" + pwd);// 向请求添加 HTTP 头,POST如果有数据要添加
}
以上实现的是在输入框通过获取/失去焦点时触发事件函数
正常情况应该是在失去焦点后前台将数据发送到后台校验,再获取后台返回的数据显示在前台,但运行时输入框失去焦点后浏览器控制台报错500。
问题出现在第4步发送数据send(“upwd” + pwd)
少了一个等号导致不能发送正确数据send(“upwd=” + pwd),找了半天才发现,心态裂了