When passing a Chinese string through AJAX, the Chinese string must be encoded as unicode. Generally, the built-in function escape () of JS is used (). however, a better function is found to determine the function for converting Chinese characters to unicode encoding.
The code is as follows: |
Copy code |
Function uniencode (text) { Text = escape (text. toString (). replace (/\ +/g, "% 2B "); Var matches = text. match (/(% ([0-9A-F] {2})/gi ); If (matches) { For (var matchid = 0; matchid <matches. length; matchid ++) { Var code = matches [matchid]. substring (1, 3 ); If (parseInt (code, 16) >= 128) { Text = text. replace (matches [matchid], '% u00' + code ); } } } Text = text. replace ('% 25',' % u0025 '); Return text; }
|
Of course, the server needs to perform the second transcoding of the encoded string. Convert the string into UTF-8 encoding.
The code is as follows: |
Copy code |
Function convert_int_to_utf8 ($ intval) { $ Intvalintval = intval ($ intval ); Switch ($ intval) { // 1 byte, 7 bits Case 0: Return chr (0 ); Case ($ intval & 0x7F ): Return chr ($ intval ); // 2 bytes, 11 bits Case ($ intval & 0x7FF ): Return chr (0xC0 | ($ intval> 6) & 0x1F )). Chr (0x80 | ($ intval & 0x3F )); // 3 bytes, 16 bits Case ($ intval & 0 xFFFF ): Return chr (0xE0 | ($ intval> 12) & 0x0F )). Chr (0x80 | ($ intval> 6) & 0x3F )). Chr (0x80 | ($ intval & 0x3F )); // 4 bytes, 21 bits Case ($ intval & 0x1FFFFF ): Return chr (0xF0 | ($ intval> 18 )). Chr (0x80 | ($ intval> 12) & 0x3F )). Chr (0x80 | ($ intval> 6) & 0x3F )). Chr (0x80 | ($ intval & 0x3F )); } } |
Supplement: solutions to Chinese garbled characters in get requests
Generally, the Tocant url encoding is ISO-8859-1 (check that the Connector node in tocat/conf/server. xml does not write URIEncoding = "xxxxxx") as follows:
The code is as follows: |
Copy code |
<Connector port = "8080" protocol = "HTTP/1.1" ConnectionTimeout = "20000" RedirectPort = "8443" type = "regxph" text = "yourobjectname"/> |
If we write the following code in servlet:
String username = request. getParameter ("name"); // name is the parameter from the get request. Here the byte code from the get request has been converted to the code of the ISO-8859-1, decoding error
Byte [] B = username. getBytes ("iso-8859-1"); // so you need to re-convert to bytecode, and then use the correct encoding method to decode, the correct encoding method is the jsp that page encoding method,
The code is as follows: |
Copy code |
Username = new String (B, "GBK "); System. out. print (username ); |