一、ajax.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ajax</title>
</head>
<body onload="init()">
<script type="text/javascript">
//浏览器兼容性
function init() {
//表示当前浏览器不是ie,如ns,firefox
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest(); //获取浏览器内置对象
} else if (window.ActiveXObject) {//当前是ie浏览器
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
//异步请求
function showInfo(){
xmlHttp.open("get","welcome.jsp",true); //发送异步请求 到 welcome.jsp
xmlHttp.onreadystatechange = function () { //处理状态
if (xmlHttp.readyState == 4){ //所有任务完成 状态为4
infoDiv.innerHTML = xmlHttp.responseText; //重提交目标中获取输出文本内容
}
}
xmlHttp.send();
}
</script>
欢迎使用AJAX。。。<hr>
<input type="button" value="跳转" onclick="showInfo()">
<div id="infoDiv"></div>
</body>
</html>
二、welcome.jsp
三、点击提交 异步请求