AJAX学习笔记3练习_biubiubiu0706的博客-CSDN博客
在Tomcat10来说,AJAX GET或者POST接收响应都不存在乱码问题
对于Tomcat9来说
前端测试代码
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试AJAX乱码问题</title>
</head>
<body>
<script type="text/javascript">window.onload=function (){//GETdocument.getElementById("btn1").onclick=function(){var xhr=new XMLHttpRequest();xhr.onreadystatechange=function (){if(this.readyState==4){if(this.status==200){document.getElementById("mydiv").innerHTML=this.responseText}else{alert("请求发送失败")}}}var username=document.getElementById("username").valuexhr.open("get","/ajax/luanma?username="+username,true)xhr.send()}//POSTdocument.getElementById("btn2").onclick=function(){var xhr=new XMLHttpRequest();xhr.onreadystatechange=function (){if(this.readyState==4){if(this.status==200){document.getElementById("mydiv").innerHTML=this.responseText}else{alert("请求发送失败")}}}var username=document.getElementById("username").valuexhr.open("post","/ajax/luanma",true)xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")xhr.send("username="+username)}}
</script><input type="text" id="username">
<button id="btn1">发送AJAX GET请求,测试乱码问题</button>
<button id="btn2">发送AJAX POST请求,测试乱码问题</button><div id="mydiv"></div>
</body>
</html>
后端测试代码
package com.web;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;
import java.io.PrintWriter;/*** @author hrui* @date 2023/9/4 6:02*/
@WebServlet("/luanma")
public class AjaxRequestLuanma extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//接收的中文会不会乱码String username = req.getParameter("username");System.out.println(username);//响应中文会乱码吗PrintWriter writer = resp.getWriter();writer.print("GET响应");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//接收的中文会不会乱码String username = req.getParameter("username");System.out.println(username);//响应中文会乱码吗PrintWriter writer = resp.getWriter();writer.print("POST响应");}
}
解决方式
package com.web;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;
import java.io.PrintWriter;
import java.sql.ResultSet;/*** @author hrui* @date 2023/9/4 6:02*/
@WebServlet("/luanma")
public class AjaxRequestLuanma extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//接收的中文会不会乱码String username = req.getParameter("username");System.out.println(username);//响应中文会乱码吗resp.setContentType("text/html;charset=utf-8");PrintWriter writer = resp.getWriter();writer.print("GET响应");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//接收的中文会不会乱码req.setCharacterEncoding("utf-8");//解决body体乱码问题String username = req.getParameter("username");System.out.println(username);//响应中文会乱码吗resp.setContentType("text/html;charset=utf-8");PrintWriter writer = resp.getWriter();writer.print("POST响应");}
}