欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jQuery.Ajax回调函数success返回字符串中文乱码问题

程序员文章站 2022-07-12 19:10:07
...

$.post请求到 /a3 成功后获取返回的中文字符串 data 结果在浏览器输出 出现乱码

js代码

$('#update-button').on('click',function () {
        var stuID = $('#stuID').val();
        var num = $('#rf_num').val();
        var password = $('#rf_password').val();
        var realName = $('#rf_name').val();
        var college = $('#rf_college').val();
        var grade = $('#rf_grade').val();
        var profession = $('#rf_profession').val();
        var classNum = $('#rf_class').val();
        var tel = $('#rf_tel').val();
        $.ajax({
            type:"POST",
            url:getRootPath()+"/manager/updateStudent.action",
            data:{
                stuId:stuID,
                num:num,
                password:password,
                realName:realName,
                college:college,
                grade:grade,
                profession:profession,
                classNum:classNum,
                tel:tel
            },
            success: function (data) {
                alert(data);
                console.log(data);
            }
        });
    });

java 代码

@RequestMapping("updateStudent")
    @ResponseBody
    public String updateStudent(Integer stuId, String num, String password, String realName, String college, String grade,
                                String profession, String classNum, String tel){
        //先改学生信息
        Sstudent student = studentService.getStudentById(stuId);
        student.setRealName(realName);
        student.setStuGrade(grade);
        student.setStuClass(classNum);
        student.setStuPhone(tel);
        //获取所有专业匹配一下
        List<Professional> professionalList = professionalService.selectAll();
        for (Professional professional : professionalList) {
            if (professional.getProfessionalName().equals(profession)){
                student.setProfessionalId(professional.getProfessionalId());
                student.setStuCollegeId(professional.getCollegeId());
                break;
            }
        }
        //User信息
        Uuser user = userService.getUserById(stuId);
        user.setUsername(num);
        user.setPassword(password);

        //更改两个表 1.学生表 2.user表
        studentService.updateStudent(student);
        userService.updateUser(user);
        return "修改成功";

jQuery.Ajax回调函数success返回字符串中文乱码问题
解决方法:在web.xml配置文件中加入spring的filter过滤器

  <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

如果在web.xml中加入spring编码过滤器还是无效,就在springmvc-servlet.xml配置文件中加入以下代码

<!--支持mvc注解-->
    <mvc:annotation-driven>
        <!--    json乱码解决-->
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                        <property name="failOnEmptyBeans" value="false"/>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

加入之后就成功解决

相关标签: ajax