Text garbled by extjs and Ajax

Source: Internet
Author: User

Garbled characters occur when ext is used, including garbled characters in the data submitted by the client to the server and the data returned by the server to the client, the following is a brief analysis of the cause of Garbled text.

During a page view, the client initiates a browser request to a URL, and the server parses the request. In terms of character encoding resolution, the server first checks the character encoding settings on the page, that is, <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>, so that the page displays the characters encoded as UTF-8, the server will output the page with the UTF-8 encoding, and if the page does not show the Declaration, there is a default character encoding in the server, such as gb2312, for files that are not declared, he will use the default encoding output, this time, if the page output is a UTF-8, it will appear garbled.

In the process of submitting data in ext, ext framework is all UTF-8 encoding, and the data submitted through JSON is also UTF-8 encoding, so all files are required to be UTF-8 encoding.

Solution to Garbled text

The following items must be done in the usual coding process:

(1) Add response. setcontenttype ("text/html") to the Java background file ");
Response. setcharacterencoding ("UTF-8 ");

(2) display the declared character encoding in the file. <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

(3) For garbled Characters During submission, you can modify the content-type settings in the Request Header in Ext. The specific method is Ext. lib. ajax. defaultpostheader + = "; charset = UTF-8 ";

Finally, the problem may also occur in the file storage format. Is when you save the code file, if the development environment by default save is not the UTF-8 format, it will also have problems! Therefore, there must be a requirement for the development environment.

Development Environment Configuration

If you use NotePad to write and save the code file (. JS), then there will be a problem, mainly because the default encoding format of notepad in Windows is ANSI, in this way, the encoding format is basically different from that in the current webpage. Therefore, the encoding format is incorrect. The solution is to open the XX. js file and save it as, and select the UTF-8 encoding format in the SAVE type.

Of course, myeclipse is still used in the mainstream, so let's talk about what to configure in myeclipse.

Preferences-> General-> content types select text to edit the following default encoding as UTF-8 and then update.
Preferences-> General-> content types-> editors-> text editors-> encoding under spelling select default (UTF-8)
Preferences-> General-> content types-> select other: UTF-8 for text file encoding in Workspace

If garbled characters still appear, use
System. Out. Print (request. getcharacterencoding ());
System. Out. Print (response. getcharacterencoding ());
 
Check whether the request or response encoding is not UTF-8
If not, use the filter in Web. XML to solve the problem.
For example
<Filter>
<Description> NO </description>
<Display-Name> encodingfilter </display-Name>
<Filter-Name> encodingfilter </filter-Name>
<Filter-class>
Com. Hope. Filters. setcharacterencodingfilter
</Filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
Or set
<Globalization responseencoding = "UTF-8" fileencoding = "UTF-8" requestencoding = "UTF-8"/>

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/linshutao/archive/2010/10/22/5959973.aspx

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.