黑马程序员
AJAX–概念
AJAX–实现–原生JS方式1
AJAX–实现–原生JS方式2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AJAX--实现--原生JS方式1
</title>
<script src="js/jquery-3.3.1.min.js"></script>
</head>
<body>
<script>
function btn_ajax() {
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","AjaxServlet?name=tom",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
}
</script>
<input type="button" value="发送异步请求" onclick="btn_ajax()">
<span id="myDiv"></span>
<input>
</body>
</html>
package cn
.itcast
.web
.servlet
;
import javax
.servlet
.ServletException
;
import javax
.servlet
.annotation
.WebServlet
;
import javax
.servlet
.http
.HttpServlet
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
import java
.io
.IOException
;
@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
protected void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
request
.setCharacterEncoding("utf-8");
response
.setContentType("text/html;charset=utf-8");
String name
= request
.getParameter("name");
System
.out
.println(name
);
response
.getWriter().write("获取到异步参数:"+name
);
}
protected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
doPost(request
,response
);
}
}