- 实现POST方法
var xmlhr;
if (window.XMLHttpRequest) {
xmlhr = new XMLHttpRequest();
} else {
xmlhr = new ActiveXObject("Microsoft.XMLHTTP");
}
// 在这里将open()函数的第三个参数设置为false,表示ajax同步执行
xmlhr.open("POST","test.php",true);
xmlhr.onreadystatechange = function () {
if (xmlhr.readyState == 4 && xmlhr.status == 200 || xmlhr.status == 304) {
if (xmlhr.responseText == "true") {
alert("成功");
}else {
alert("成功");
}
}
}
// 参数
var data = "email=" + emailinput.value;
xmlhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhr.send(data);
http://blog.csdn.net/interesting_wang/article/details/79506907 - 实现GET方法
var xmlhr;
if (window.XMLHttpRequest) {
xmlhr = new XMLHttpRequest();
} else {
xmlhr = new ActiveXObject("Microsoft.XMLHTTP");
}
// 生成路由
var url = "./action/user.action.php?action=checkRepeatEmail&email=" + emailinput.value;
// GET方式请求服务器端数据
xmlhr.open("GET",url,true);
xmlhr.send();
xmlhr.onreadystatechange = function () {
if (xmlhr.readyState == 4 && xmlhr.status == 200 || xmlhr.status == 304) {
if (xmlhr.responseText == "true") {
alert("成功");
}else {
alert("成功");
}
}
}
原生js实现ajax
最新推荐文章于 2023-05-08 21:48:44 发布