<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function fun(){
//ajax核心xmlHttpRequest对象
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//建立连接
/*
async:true(异步)或 false(同步)
*/
xhttp.open("get","ser1?uname=lwx",true);
//发送请求
/*
get:无参 参数在url后
post:有参数
*/
xhttp.send();
//接受处理返回结果
//就绪状态改变时
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
}
</script>
</head>
<body>
<!-- 原生ajax-->
<button type="button" value="ajax1" onclick="fun()">btn</button>
<input>
</body>
</html>
package com.xxx;
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("/ser1")
public class ser1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uname=req.getParameter("uname");
System.out.println(uname);
resp.getWriter().write(uname);
}
}