[Java] [jquery] ajax garbled problem, javajqueryajax
1. It is recommended that the HTML and JSP files adopt the UTF-8 format.
2. Add the following to the HTML head:
<META http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
3. Add a JSP File Header
<% @ Page contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
4. Filter can also be used at and:
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
If (req. getHeader ("X-Requested-")! = Null & req. getHeader ("X-Requested-With"). inclusignorecase ("XMLHttpRequest ")){
Request. setCharacterEncoding ("UTF-8 ");
} Else {
Request. setCharacterEncoding ("gbk ");
}
Chain. doFilter (request, response );
}
5. Add URIEncoding = "UTF-8" under the Connector tag in your tomcat or jboss server. xml file"
This is the key, general middleware default encoding format is iso8859-1, iso8859-1 can display Chinese, but the support for Chinese is not very good, Some uncommon words can not be displayed.
If this step is not set, you need to use the following method for transcoding: new String (request. getParameter ("chnlName"). getBytes ("iso8859-1"), "UTF-8 ")
Struts2 + jqueryAjax Chinese garbled problem, java second kill this problem
In the conf folder of tomcat: server. xml
Connector port = "8080" protocol = "HTTP/1.1"
ConnectionTimeout = "20000"
RedirectPort = "8443"/> label Add: URIEncoding = "UTF-8"
Modify it as follows:
<Connector port = "8080" protocol = "HTTP/1.1"
ConnectionTimeout = "20000"
RedirectPort = "8443" URIEncoding = "UTF-8"/>
Can you try it?
JQuery ajax Chinese garbled
1. The front and back-end encoding should be unified;
2. When using gb2312 encoding, do not use $. get () or $. post () of jquery for ajax submission, because the two methods are UTF-8 by default;
3. Use $. add contentType: "pplication/x-www-form-urlencoded; charset = GB2312" to ajax () and write it in the following format to avoid garbled code in most cases:
$. Ajax ({
Type: "POST ",
ContentType: "pplication/x-www-form-urlencoded; charset = GB2312 ",
Url: "XXX",
Data :{},
Success: function (msg ){
Alert (msg );
}
});