[Java] [jquery] ajax garbled problem, javajqueryajax

Source: Internet
Author: User
Tags jboss server

[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 );
}
});
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.