Ajax Chinese garbled problem

Source: Internet
Author: User

<%@ page contenttype= "text/html;charset=gb2312%>
If you use a servlet, add
Response.setcontenttype ("text/html;charset=gb2312");
Request.setcharacterencoding ("gb2312");
And a better way is to add a filter
in which to join
Response.setcontenttype ("text/html;charset=gb2312");
Request.setcharacterencoding ("gb2312");
Everything's settled.

Again, uploading data from the client must be encoded at the server.
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.


<script>
var oxmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
Oxmlhttp.open ("Get", "ASP tutorial x.cc/content.aspx" >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
<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:

Java code
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:

  

Java code
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 solution that is once and for all, adding a filter.
The code
String username = new String (Request.getparameter ("username") is added when the Commit method is written at the server for Get time. GetBytes ("iso8859_ 1 ")," gb2312 ");   
as it should when post
string username = new String (Request.getparameter (" username "). GetBytes ("Iso8859_1"), "Utf-8");  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.