Summary of various solutions to Ajax Chinese garbled code

Time:2021-10-27

Because xmlhttp uses Unicode encoding to upload data, while the general page uses GB2312, it causes garbled code when displaying the page. While xmlhttp returns UTF-8 encoding when obtaining the page, it causes garbled code when displaying

One solution is to use encodeuricomponent and modify the content type to “application / x-www-form-urlencoded” to uniformly encode the data into URL format. Of course, you can also specify the code, such as “application / x-www-form-urlencoded; charset = UTF-8”,

One of the solutions

example:

Copy codeThe code is as follows:
http_request = new ActiveXObject(“Msxml2.XMLHTTP”);
http_request.setrequestheader(“content-type”,”application/x-www-form-urlencoded; charset=utf-8″);

Solution 2
Is to display the declaration as GB2312 in the PHP file

Copy codeThe code is as follows:
header(“Content-Type:text/html;charset=GB2312”);

Transcoding the Chinese sent to the server

as follows

Copy codeThe code is as follows:
<?php
header(“Content-Type:text/html;charset=GB2312”);
if($_POST[‘content’])
{
$_POST[“content”]=iconv(“UTF-8″,”gb2312”,$_POST[“content”]);
Print (“the content is” $_post [‘content ‘]);
}
else
{
Print (“no content sent”);
}
?>

Therefore, this can solve the problem of garbled code

Ajax Chinese garbled code in JSP

The parameters in the sending path are in Chinese, and the received parameter values in the server section are garbled
For example:
Var url = “a.jsp? Name =” Xiao Li “;
xmlHTTP.open (“post”,url,true);
terms of settlement:
Use the escape () or encodeuri () method provided by JavaScript
For example:
client:

Copy codeThe code is as follows:
Var url = “a.jsp? Name =” Xiao Li “;
url=encodeURI(url);
url=encodeURI(url); // Twice, it’s crucial [I don’t know exactly why]
/********************************************/

It was also written as var url = “a.jsp? Name = escape (” Xiao Li “)”;
The function is similar to the encodeuri method.

Copy codeThe code is as follows:
/********************************************/
xmlHTTP.setrequestheader(“cache-control”,”no-cache”);
xmlHTTP.setrequestheader(“Content-Type”,”application/x-www-form-urlencoded”);
Xmlhttp.setrequestheader (“contenttype”, “text / HTML; charset = uft-8”) / / specifies the encoding format of the sent data
xmlHTTP.open (“post”,url,true);

Server side:

Copy codeThe code is as follows:
String name = request.getParameter(“name”);
name = java.net.URLDecoder.decode(“name”, “UTF-8”);

2) The returned responseText or responsexml value contains Chinese, which is garbled
Reason: Ajax decodes the responseText or responsexml value in UTF-8 format. If the data sent by the server segment is not in UTF-8 format, the received responseText or responsexml value may be garbled.
Solution: specify the format of sending data on the server:
In the JSP file:
response.setContentType(“text/text;charset=UTF-8”);// The returned is a TXT text file
or
response.setContentType(“text/xml;charset=UTF-8”);// Returned XML file

gb2312 Get data using jQuery Ajax gb2312 yes Jsp: <% @ page contenttype = “application / JSON” pageencoding = “GB2312″% > the JSON used here should be the same as text / HTML
APS: response. Charset = “GB2312” (reference network not tested)
php:header(“Content-Type:text/html; charset=gb2312”); (reference network not tested)
HTML: unresolved
gb2312 Get data using jQuery Ajax utf-8 no  
utf-8 Get data using jQuery Ajax utf-8 no  
utf-8 Get data using jQuery Ajax gb2312 yes Jsp: <% @ page contenttype = “application / JSON” pageencoding = “GB2312″% > the JSON used here should be the same as text / HTML
APS: response. Charset = “GB2312” (reference network not tested)
php:header(“Content-Type:text/html; charset=gb2312”); (reference network not tested)
HTML: unresolved