原生Ajax的怎么用?
<script>
function createXMLHttpRequest() {
var xmlhttp;
try {
//先直接创建XMLHttpRequest
xmlhttp = new XMLHttpRequest();
} catch (e) {
try {
//如果有异常,创建不成功
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
//如果还有异常
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("您的浏览器不支持ajax");
return;
}
}
}
return xmlhttp;
}
// 发送原生Ajax请求的方法
function checkName(obj) {
//得到XMLHttpRequest对象
var xmlhttp = createXMLHttpRequest();
//开启请求
xmlhttp.open("post", "${pageContext.request.contextPath}/stu/registCheck");
xmlhttp.onreadystatechange = function() {
//如果响应成功
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsonObj = JSON.parse(xmlhttp.responseText);
var span = document.getElementById("name2");
if (jsonObj.userExsit) {
var input = document.getElementById("input");
input.isDisabled = "true";
span.innerHTML = "<font color=‘red‘>" + jsonObj.msg + "</font>";
} else {
span.innerHTML = "<font color=‘green‘>" + jsonObj.msg + "</font>";
}
}
}
//设置请求头,意思是以post的方式提交表单数据,编码格式为utf-8
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
//发送请求
xmlhttp.send("name=" + obj);
}
</script>
原生Ajax的怎么用?
,温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/43110.html