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响应");
    }
}
相关推荐
jbjhzstsl11 分钟前
lv_ffmpeg学习及播放rtsp
学习·ffmpeg
青い月の魔女19 分钟前
数据结构初阶---二叉树
c语言·数据结构·笔记·学习·算法
网络安全(king)21 分钟前
网络安全攻防学习平台 - 基础关
网络·学习·web安全
qq_5895681027 分钟前
node.js web框架koa的使用
笔记·信息可视化·echarts
虾球xz1 小时前
游戏引擎学习第59天
学习·游戏引擎
stm 学习ing1 小时前
HDLBits训练6
经验分享·笔记·fpga开发·fpga·eda·verilog hdl·vhdl
枫零NET2 小时前
学习思考:一日三问(学习篇)之匹配VLAN
网络·学习·交换机
stm 学习ing2 小时前
HDLBits训练4
经验分享·笔记·fpga开发·课程设计·fpga·eda·verilog hdl
沐泽Mu2 小时前
嵌入式学习-QT-Day07
c++·qt·学习·命令模式
沐泽Mu2 小时前
嵌入式学习-QT-Day09
开发语言·qt·学习