Analysis of AJAX garbled characters and error Solutions

Source: Internet
Author: User

One is garbled characters, and the other is javascript system errors:-1072896658. The row XX. innerHTML = xmlhttp. responseText; is displayed.
In fact, it is caused by inconsistent encoding. The solution is to add the specified character encoding command.

1. What is character encoding?
For details, refer.
In layman's terms: the "I" you see is a small image. Every Chinese character is represented by a 4650 character code. These images are stored in a place called a font library, you can find the image No. 4650 in the font, that is, "I". In fact, the font is the font of windows, the font of, And the font of. This is an article, you can use and. No matter which one you use, the corresponding encoding of "I" remains unchanged. You can change the font. The displayed "I" font is different, but it is still a "I" font.

Ii. Problem generation.
The encoding format is used to generate various encodings. Typical Chinese characters include GB2312 simplified, BIG5 traditional, and Unicode. The problem occurs here. In GB2312, 4650 is "I" and 6211 is in Unicode. 4650 corresponds to "bytes" in Unicode ".
Therefore, you sent "I" in the server's asp and jsp code using GB2312 encoding. When you arrived at the browser, the browser thought it was Unicode and translated it as "Unicode ". This is garbled.
Note: Unicode in this article takes UTF-8 as an example. UTF-8 defining portals: http://zh.wikipedia.org/zh/UTF-8

3. Solutions.
Note that JavaScript uses Unicode UTF-8 as the internal code for unreliable messages.
Therefore, if Ajax is used, it is better to render the page and specify the Encoding As UTF-8, saving the transformation. If the encoding is unified, garbled characters will naturally disappear.

4. Specific methods.
For server scripts, start with the script:
PHP: header ('content-Type: text/html; charset = UTF-8 ');
ASP: Response. Charset = "UTF-8"
JSP: response. setHeader ("Charset", "UTF-8 ");
Note ASP, many of which are Response. Charset ("UTF-8"), which is incorrect, Charset is attribute not method, misleading many people.
For HTML
Copy codeThe Code is as follows:
<Head>
<Meta name = "description" content = "Free Web tutorials"/>
<Meta name = "keywords" content = "HTML, CSS, XML, JavaScript"/>
<Meta name = "author" content = "Hege Refsnes"/>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
</Head>

Charset = encoding name.
For <script>
<Scripte type = "text/javascript" charset = "UTF-8">
However, in practical application, especially when page conversion, will appear before a page is UTF-8, after the page suddenly jumped to the GB2312, and then garbled, what should I do?
Write data at the beginning of the Server asp (PHP, JSP self-check method) script
Session. CodePage = 65001
Keep UTF-8 in this Session.
If you want to unify into GB2312, then remember to replace the above UTF-8 with GB2312, 65001 with 936.
These three steps can basically solve the garbled problem.

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.