tylor说的问题:简单的ajax例子
发布网友
发布时间:2022-04-21 21:45
我来回答
共1个回答
热心网友
时间:2022-04-21 23:15
var xmlHttpRequest;
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
xmlHttpRequest = new XMLHttpRequest();
} else if (window.ActionObject) {
xmlHttpRequest = new ActiveObject("Msxml2.XMLHTTP");
} else {
xmlHttpRequest = new ActiveObject("Microsoft.XMLHTTP");
}
if (xmlHttpRequest == null) {
alert("对不起您的浏览器不支持此功能,请换用IE浏览器");
}
}
function ajax(ajax) {
createXMLHttpRequest();
xmlHttpRequest.onreadystatechange = function() {
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
document.getElementById("ajax").innerHTML = xmlHttpRequest.responseText;
}
}
xmlHttpRequest.open("GET",
"AjaxServlet?method=" + ajax, false);
xmlHttpRequest.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
xmlHttpRequest.send();
return true;
}
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
PrintWriter out = response.getWriter();
out.print("ajax");