Garbled characters encountered during Ajax Development

Source: Internet
Author: User

Garbled data is submitted during Ajax development.

Excerpt

# Re: garbled data submitted during Ajax development 2006-09-06 | the crow flew in the dark

In fact, all problems come from inconsistent encoding:
1. By default, the HTTP/POST method uses "X-WWW-form-urlencoded" for encoding. It plays the same role as encodeuri in JavaScript;
2, after such encoding content, to the background, the characters are UTF-8 encoding format;
3. If your Servlet/jsp specifies that the content-type used by the output is the UTF-8, congratulations, you don't need to perform special encoding and decoding operations on these parameters, it must be normal;
4. Otherwise, you need to decode and encode the parameters. For example, if your page is GBK encoded, you need to write the following code:
String spara = new string (request. getparamter ("test"). getbytes ("iso-8859-1"), "GBK ");
Gb2312 encoding can also be used;
5. If you are working in eclipse, pay attention to the attributes of your Servlet and JSP files. The encoding must be the same as that set in Content-Type, otherwise, javac will incorrectly encode your file. At this time, the character string of the bytecode file itself is incorrect, and the output is not used;
6. Make a summary:
6.1 The Servlet/JSP file encoding must be consistent with the Content-Type output; otherwise, additional encoding and decoding steps must be added;
6.2, HTTP/post and encodeuri encoding are passed in UTF-8;
6.3. The normal window. Open (...), that is, the HTTP/get method, is passed in encoding consistent with the page code running the script;
6.4, the background decoding must be decoded through the iso-8859-1, and then use your target encoding;
6.5. All problems can be basically solved.

Reply to more comments

 

To solve Ajax garbled characters, you can use the escape () function.
The escape method cannot be used to encode uniform resource identifiers (URIs. Use the encodeuri and encodeuricomponent methods for encoding.

 

 

 

To solve the Garbled text problem in ASP, you only need to add <% response. contenttype = "text/html; charset = gb2312" %> to the server.

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.