Ajax Chinese garbled characters often occur in Chinese. In fact, ajax Chinese garbled characters will not occur as long as you pay attention to them. Note that the front-end and back-end encoding are consistent. you are using Chinese. while ajax uses UTF-8 for data transmission, and it is best to use escape or urlcode for ajax get methods,
Copy codeThe Code is as follows: <% @ page contenttype = "text/html; charset = gb2312 %>
If servlet is used, addCopy codeThe Code is as follows: response. setcontenttype ("text/html; charset = gb2312 ");
Request. setcharacterencoding ("gb2312 ");
Another better way is to add a filter.
AddCopy codeThe Code is as follows: response. setcontenttype ("text/html; charset = gb2312 ");
Request. setcharacterencoding ("gb2312 ");
Everything is done
After uploading data from the client, you must perform encoding conversion on the server.Copy codeThe Code is as follows: string param = request. getparamter ("param ");
Param = new string (param. getbytes ("iso-8859-1"), "gb2312 ");
Now it's all Chinese.
Note that the front and back-end encoding is the same. You use Chinese characters, while ajax uses UTF-8 for data transmission.Copy codeThe Code is 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>
Set the client file encoding to gb2312, as shown in the following code:
Html codeCopy codeThe Code is 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 sent url address or the request content sent using the post method. Remember!
The jsp tutorial file on the server is also set to gb2312 encoding format, as shown in the following code:
Jsp code
Or set the response header, as shown in the following code:Copy codeThe Code is as follows: response. setheader ("content-type", "text/html; charset = gb2312 ");
The principles are the same.
The most important thing is to re-encode the obtained string when obtaining the parameter, as shown in the following code:Copy codeThe Code is as follows: string username = new string (request. getparameter ("username"). getbytes ("iso8859_1"), "gb2312 ");
Here, username is the received parameter.
Directly use out. print (username); then you can return Chinese to the client. You can directly use the xmlhttp. responsetext attribute on the client to directly use the returned Chinese!
In the attachment, I used a small example for testing. I passed the test in tomcat6.0 and resin2.1.8!
In fact, there is a permanent solution to add a filter.
Add this code when the submission method is get written on the server from time to time.Copy codeThe Code is as follows: string username = new string (request. getparameter ("username"). getbytes ("iso8859_1"), "gb2312 ");
This should be the case for post.Copy codeThe Code is as follows: string username = new string (request. getparameter ("username"). getbytes ("iso8859_1"), "UTF-8 ");