Analysis of Ajax garbled code and its error solution

Time:2021-9-12

One is the occurrence of garbled code, and the other is the occurrence of JavaScript system error: – 1072896658. Prompt inXX.innerHTML=xmlhttp.responseText;This line.
In fact, it is all caused by inconsistent coding.The solution is to add the specified character encoding instruction.

1、 What is character encoding
As defined in: http://zh.wikipedia.org/wiki/%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81 。
Generally speaking, the “I” you see is a small picture, and each Chinese character corresponds to a code of 4650. These pictures are stored in a place called the font library. If you find the picture No. 4650 in the font library, it is “I”. In fact, the font library is the font of windows, such as the song font library, YaHei font library, etc. for an article, you can use the song font and imitation song font, No matter what kind of code is used, the corresponding code of “I” is unchanged. You can change the font. The displayed “I” font is different, but it is still a “I”.

2、 Problem generation.
Coding has coding format, so there are various codes. Typical Chinese characters include GB2312 simplified, Big5 traditional and Unicode. The problem arises here. 4650 is “I” in GB2312 and 6211 in Unicode. 4650 corresponds to “䙐” in Unicode.
Therefore, you send “I” with GB2312 code in ASP and JSP of the server. When you arrive at the browser, the browser thinks it is Unicode and translates it as “I”. This is garbled.
be careful:Unicode in this article only takes UTF-8 as an example. UTF-8 defines the portal: http://zh.wikipedia.org/zh/UTF-8

3、 Solutions.
One more thing to note: according to the unreliable message, JavaScript uses Unicode UTF-8 as the inner code.
Therefore, if AJAX is used, it is better to specify the encoding of the rendered page as UTF-8, which saves the conversion. Unified coding, naturally there is no garbled code.

4、 Specific methods.
For the server 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 in ASP, many places are response. Charset (“UTF-8”), which is incorrect. Charset is an attribute rather than a method, which misleads 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 = code name.
For < script >
<scripte type=”text/javascript” charset=”UTF-8″>
However, in practical applications, especially during page conversion, the previous page is UTF-8, and the subsequent page suddenly jumps to GB2312, and then the code is garbled. What should I do?
Write in 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 it into GB2312, remember to replace the UTF-8 with gb231265001 and 936.
The completion of these three parts can basically solve the problem of garbled code.