AJAX学习笔记4解决乱码问题

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 (){
        //GET
        document.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").value
            xhr.open("get","/ajax/luanma?username="+username,true)

            xhr.send()
        }
        //POST
        document.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").value
            xhr.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 {

    @Override
    protected 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响应");
    }

    @Override
    protected 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 {

    @Override
    protected 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响应");
    }

    @Override
    protected 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响应");
    }
}
相关推荐
huangkj-henan16 分钟前
DA217应用笔记
笔记
Young_2022020217 分钟前
学习笔记——KMP
笔记·学习
行然梦实33 分钟前
学习日记_20241110_聚类方法(K-Means)
学习·kmeans·聚类
马船长39 分钟前
制作图片木马
学习
秀儿还能再秀1 小时前
机器学习——简单线性回归、逻辑回归
笔记·python·学习·机器学习
WCF向光而行1 小时前
Getting accurate time estimates from your tea(从您的团队获得准确的时间估计)
笔记·学习
wang09071 小时前
工作和学习遇到的技术问题
学习
Li_0304063 小时前
Java第十四天(实训学习整理资料(十三)Java网络编程)
java·网络·笔记·学习·计算机网络
心怀梦想的咸鱼3 小时前
ue5 蓝图学习(一)结构体的使用
学习·ue5
kali-Myon3 小时前
ctfshow-web入门-SSTI(web361-web368)上
前端·python·学习·安全·web安全·web