//async:true(异步)或 false(同步)
function ajaxPOSTFun(url, params, callback) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp) {
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(params);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback(xmlhttp.responseText);
}
}
} else {
alert("Your browser does not support XMLHTTP.");
}
}
//example
function getPage(param) {
var url = "/linkskip/demo/demo" + param + ".html";
ajaxPOSTFun(url, "num=" + param,
function(data) {
window.document.write(data);
});
}
//get
function ajaxGETFun(url, params, callback) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url + "?" + params, true);
xmlhttp.send();
}
自己封装的一个最简单的ajax
最新推荐文章于 2025-08-13 10:52:50 发布