Solve Chinese garbled characters in Ajax

Source: Internet
Author: User

Ajax does not support a variety of character sets, its default character set is UTF-8, so the application of Ajax technology in the program should be timely encoding conversion, otherwise Chen Xu appears in the Chinese characters will become garbled. In general, Chinese garbled characters can be generated in the following two cases.

1. The sending path parameters contain Chinese characters and garbled characters are generated when the server accepts the parameter values.

You can submit data to the server in either the get method or the POST method. The methods for submitting data using different methods are different. When the Server accepts parameters, the methods for resolving Chinese garbled characters are different. The solution is as follows:

(1) When receiving data submitted using the get method, convert the encoding to GBK. The key code is as follows:

String Wishman = request. getparameter ("Wishman ");

Out. println ("Blessing object:" + new string (Wishman. getbytes ("ISO-8859-1"), "GBK"); // address Chinese garbled characters

(2) because the default character encoding is UTF-8 when the POST method is applied to submit data, so when receiving data submitted using the POST method, the encoding is to be converted to UTF-8, the key code is as follows:

String Wishman = request. getparameter ("Wishman ");

Out. println ("Blessing object:" + new string (Wishman. getbytes ("ISO-8859-1"), "UTF-8"); // address Chinese garbled characters

2. garbled characters are generated when the returned value of response text or responsexml contains Chinese characters.

When accepting the value of responsetext or responsexml, if it contains Chinese characters, you need to convert it to GBK or gb2312 encoding, and set the encoding format of this value page to GBK or gb2312, otherwise, garbled characters are generated.

 

Related Article

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.