Ajax Chinese garbled problem in Chinese often appear this problem, in fact, as long as a little attention will not appear Ajax Chinese garbled this thing, pay attention to the front and back of the code. You use Chinese. Ajax transmits data using Utf-8 and Ajax get methods best escape or Urlcode,
Copy Code code as follows:
<%@ page contenttype= "text/html;charset=gb2312%>
If you use a servlet, add
Copy Code code as follows:
Response.setcontenttype ("text/html;charset=gb2312");
Request.setcharacterencoding ("gb2312");
And a better way is to add a filter
in which to join
Copy Code code as follows:
Response.setcontenttype ("text/html;charset=gb2312");
Request.setcharacterencoding ("gb2312");
Everything's settled.
Again, uploading data from the client must be encoded at the server.
Copy Code code as follows:
string param = Request.getparamter ("param");
param = new String (Param.getbytes ("iso-8859-1"), "gb2312");
It's all in Chinese now.
Note that the front and rear coding is consistent. You use Chinese. and Ajax transmits data with Utf-8.
Copy Code code as follows:
<script>
var oxmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
Oxmlhttp.open ("Get", "http://dotnet.aspx.cc/content.aspx", false);
Oxmlhttp.send ()
var ostream = new ActiveXObject ("ADODB.stream");
if (ostream = null)
Alert ("Your machine does not support ADODB.stream.")
Else
{
ostream.type=1;
ostream.mode=3;
Ostream.open ();
Ostream.write (Oxmlhttp.responsebody);
ostream.position= 0;
Ostream.type= 2;
Ostream.charset= "gb2312";
var result= ostream.readtext ();
Ostream.close ();
Ostream = null;
alert (result);
}
</script>
The encoding for the client file is set to gb2312, as shown in the following code:
HTML code
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
Do not use the escape function to encode the query string in the URL address that is sent or the content of the request sent using the Post method, remember!
The JSP tutorial file on the server side is also set to the GB2312 encoding format, as shown in the following code:
JSP code
or set the header of the response, as shown in the following code:
Copy Code code as follows:
Response.setheader ("Content-type", "text/html; charset=gb2312");
The two principles are the same.
The most important thing to do is to recoding the fetch string when getting the parameters, as shown in the following code:
Copy Code code as follows:
String username = new String (Request.getparameter ("username"). GetBytes ("Iso8859_1"), "gb2312");
Where the username is the received parameter.
Direct use of out.print (username), you can return the Chinese to the client, the client directly using the Xmlhttp.responsetext attribute can directly use the returned Chinese!
In the appendix, I tested a small example, in tomcat6.0 and resin2.1.8 pass the test!
In fact, there is a once and for all solution, is to add a filter.
Add the code that the Submit method writes to the server when it is always written
Copy Code code as follows:
String username = new String (Request.getparameter ("username"). GetBytes ("Iso8859_1"), "gb2312");
This should be the case for post.
Copy Code code as follows:
String username = new String (Request.getparameter ("username"). GetBytes ("Iso8859_1"), "Utf-8");