对初学AJAX的同学会有帮助的。
----------ajax.js---------------
var request = false;
try {
request = new XMLHttpRequest();
}
catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed) {
request = false;
}
}
}
if (!request) alert("Error initializing XMLHttpRequest!");
function getCustomerInfo() {
document.getElementById("output").innerHTML = "Loading...";
document.getElementById("submit").disabled = true;
var name = document.getElementById("name").value;
var text = document.getElementById("text").value;
var key = document.getElementById("key").value;
var answer = document.getElementById("answer").value;
var poststr = "name="+name+"&text="+text+"&key="+key+"&answer="+answer;
var url = "doit.php";
request.open("POST", url, true);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send(poststr);
request.onreadystatechange = updatePage;
}
function updatePage() {
if (request.readyState == 4)
if (request.status == 200)
document.getElementById("output").innerHTML = request.responseText;
else if (request.status == 404)
alert("目标地址不存在");
else
alert("警告: 错误代码 " + request.status);
}
function getInfo() {
document.getElementById("output1").innerHTML = "Loading...";
var url = "show.php";
var d = new Date().getTime();
request.open("GET", url+"?d="+d.toString(), true);
request.onreadystatechange = updatePage1;
request.send();
}
function updatePage1() {
if (request.readyState == 4)
if (request.status == 200)
document.getElementById("output1").innerHTML = request.responseText;
else if (request.status == 404)
alert("目标地址不存在");
else
alert("警告: 错误代码 " + request.status);
}
2022-05-06 12:38:47
79KB
ajax
1