Analysis of Ajax garbled and wrong solution

Source: Internet
Author: User
It's unreliable message JavaScript is used as an UTF-8 with Unicode, but there are all sorts of bugs in the current cluttered browser environment, especially after a more cluttered Ajax

A class is garbled, the other is a JavaScript system error:-1072896658. Hint at Xx.innerhtml=xmlhttp.responsetext; This line.
In fact, because the coding is not unified cause. The workaround is to add the specified character encoding instruction.

One, what is the character encoding
definition See: http://zh.wikipedia.org/wiki/%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81.
In layman's terms: the "I" you see is a small picture, each Chinese character is, he corresponds to a code 4650, these pictures are stored in a place called the font, you find No. 4650 in the library picture, is "I", in fact, is the font of Windows fonts, italicized Word library, elegant black font and so on, An article, you can use the song body, Song, regardless of the kind, "I" the corresponding encoding is unchanged. You can change the font, the display of "my" font is different, but still a "me" word.

Second, the problem arises.
encoding has a coding format so it produces a variety of encodings, Chinese characters typically have GB2312 simplified, BIG5 Traditional, Unicode simplified. Here is the problem, in GB2312 4650 is "I", in Unicode is 6211. 4650 in Unicode corresponds to "䙐".
Therefore, you in the server asp,jsp with GB2312 code sent "Me", to the browser, the browser is considered Unicode, translation is "䙐." This is garbled.
Note: The Unicode in this article only takes UTF-8 as an example. UTF-8 Definition Portal: http://zh.wikipedia.org/zh/UTF-8

Third, the solution to the idea.
one thing to note: The unreliable message is that JavaScript is used as an inner code with Unicode UTF-8.
Therefore, if using AJAX, the best rendering page also specifies the encoding for UTF-8, the province of the conversion. Unified the code, naturally there is no garbled.

Iv. specific methods.
for the server-side script, at the beginning of the script:
Php:header (' Content-type:text/html;charset=utf-8 ');
asp:response.charset= "UTF-8"
JSP:response.setHeader ("Charset", "UTF-8");
Note that the ASP, many places is Response.Charset ("UTF-8"), this is not true, Charset is the attribute is not a method, misleading a lot of people.
for HTML

Copy Code code 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>

The


charset= the encoded name.
for <script>
<scripte type= "Text/javascript" charset= "UTF-8"
but, in practical applications, especially when page transitions, will appear before a page is UTF-8, the back page suddenly jumps to GB2312, then garbled, how to do? The
Write
session.codepage=65001
at the beginning of the server ASP (php,jsp) script is always UTF-8 in this session.
If you want to unify into GB2312, then remember to replace the UTF-8 above with gb2312,65001 for 936.
These three complete, basically can 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.