<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
window.onload = function ()
{
document.getElementById("btn").onclick = function ()
{
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function ()
{
if(ajax.readyState === 4)
{
if(ajax.status === 200)
{
document.getElementById("mySpan").innerHTML = ajax.responseText;
}
else
{
alert(ajax.status);
}
}
}
//Open channel
var username = document.getElementById("username").value;
var userCode = document.getElementById("userCode").value;
ajax.open("GET","/ajax/ajaxRequest2?username=" + username + " & amp;userCode=" + userCode,true);
//Get request submission data is submitted on the request line, directly in the format of URL?name=value &name=value &name=value
ajax.send();
//This is stipulated by the HTTP protocol
}
}
</script>
Username<input type="text" name="username" id="username">
Account<input type="text" name="userCode" id="userCode">
<br>
<button id="btn">Send AJAX</button>
<br>
<span id="mySpan"></span>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
window.onload = function ()
{
document.getElementById("btn").onclick = function ()
{
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function ()
{
if(ajax.readyState === 4)
{
if(ajax.status === 200)
{
document.getElementById("mySpan").innerHTML = ajax.responseText;
}
else
{
alert(ajax.status);
}
}
}
//Open channel
var username = document.getElementById("username").value;
var userCode = document.getElementById("userCode").value;
ajax.open("GET","/ajax/ajaxRequest2?username=" + username + " & amp;userCode=" + userCode,true);
//Get request submission data is submitted on the request line, directly in the format of URL?name=value &name=value &name=value
ajax.send();
//This is stipulated by the HTTP protocol
}
}
</script>
Username<input type="text" name="username" id="username">
Account<input type="text" name="userCode" id="userCode">
<br>
<button id="btn">Send AJAX</button>
<br>
<span id="mySpan"></span>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
window.onload = function ()
{
document.getElementById("btn").onclick = function ()
{
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = callBack;
function callBack()
{
if(ajax.readyState === 4)
{
if(ajax.status === 200)
{
document.getElementById("mySpan").innerHTML = ajax.responseText;
}
else
{
alert(ajax.status);
}
}
}
//Open channel
ajax.open("GET","/ajax/ajaxRequest2",true);
ajax.send();
}
}
</script>
<button id="btn">Send AJAX Get</button>
<span id="mySpan"></span>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
window.onload = function ()
{
document.getElementById("btn").onclick = function ()
{
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = callBack;
function callBack()
{
if(ajax.readyState === 4)
{
if(ajax.status === 200)
{
document.getElementById("mySpan").innerHTML = ajax.responseText;
}
else
{
alert(ajax.status);
}
}
}
//Open channel
ajax.open("GET","/ajax/ajaxRequest2",true);
ajax.send();
}
}
</script>
<button id="btn">Send AJAX Get</button>
<span id="mySpan"></span>
</body>
</html>
package com.bjpowernode.AJAX.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ajaxRequest2")
public class ajaxRequest2 extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String userCode = request.getParameter("userCode");
if(username != null & amp; & amp; userCode != null)
{
out.println(username + " & amp; & amp;" + userCode);
}
else
{
out.println("hahaha");
}
}
}
package com.bjpowernode.AJAX.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ajaxRequest2")
public class ajaxRequest2 extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String userCode = request.getParameter("userCode");
if(username != null & amp; & amp; userCode != null)
{
out.println(username + " & amp; & amp;" + userCode);
}
else
{
out.println("hahaha");
}
}
}