Home  >  Article  >  Web Front-end  >  How to solve the problem of sending ajax garbled characters

How to solve the problem of sending ajax garbled characters

藏色散人
藏色散人Original
2021-12-17 15:28:563003browse

Solution to sending ajax garbled characters: 1. Check whether the character interceptor is configured in web.xml; 2. Modify the jsp page to utf-8; 3. Change the ajax request to a post request.

How to solve the problem of sending ajax garbled characters

The operating environment of this article: Windows 7 system, jquery version 3.2.1, Dell G3 computer.

How to solve the problem of sending ajax garbled characters?

Solution to the problem of Chinese garbled characters when JQuery sends an ajax request:

Troubleshoot the project first:

1. Whether it is configured in web.xml Character interceptor

<filter>
  <filter-name>encodingFilter</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>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

2. Is the jsp page utf-8

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

By the way, I also add this tag and send utf-8 uniformly, but I heard it is a bit tasteless

<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″ />

3. After troubleshooting, change the ajax request to a post request

$.ajax({
   url:"list",
   type:&#39;post&#39;,
   data:{
     param:recode,
     city:locationActive
   },
   dataType: &#39;json&#39;,//要求服务器返回的是json
   success:function(res){}

Recommended learning: "ajax video tutorial"

The above is the detailed content of How to solve the problem of sending ajax garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Why can't ajax go back?Next article:Why can't ajax go back?