Ajax Chinese garbled problem solution (servlet)

Source: Internet
Author: User

Recently I have studied Ajax. I enter Chinese Characters in text on the interface, and garbled characters are returned after being sent. The Code is as follows:

HTML:
......
// In fact, charset = UTF-8 is also acceptable, because it is on the Chinese Platform
// Gb2312 is used
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

JS:
I used two methods to submit: Get and post.
The server must convert different codes according to different submission methods.
......
// Parameters to be passed
VaR querystring = "firstname =" + firstname + "& lastname =" + lastname
+ "& Birthday =" + birthday; Function
 
// Submit in get Mode
Dorequestusingget (){
Createxmlhttprequest ();
VaR url = "getandpostexample? "+ Querystring +" & timestamp ="
+ New date (). gettime ();
XMLHTTP. onreadystatechange = handlestatechange;
XMLHTTP. Open ("get", URL, true );
XMLHTTP. Send (null );
}

// Submit in post Mode
Function dorequestusingpost (){
Createxmlhttprequest ();
VaR url = "getandpostexample? Timestamp = "+ new date (). gettime ();
XMLHTTP. Open ("Post", URL, true );
XMLHTTP. onreadystatechange = handlestatechange;
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XMLHTTP. Send (querystring );
}

Servlet:
Response. setcontenttype ("text/XML ");

// This must be set. The settings here should be the same as those in HTML, but I am here
// With the uft-8, the results are the same.
Response. setcharacterencoding ("gb2312 ");

// When using the POST method, you must set it to UTF-8; otherwise, garbled characters will occur.
String firstname = new string (request. getparameter ("firstname"). getbytes ("ISO-8859-1"), "UTF-8 ");

// When using the get method, set it to gb2312; otherwise, garbled characters are required.
String lastname = new string (request. getparameter ("lastname"). getbytes ("ISO-8859-1"), "gb2312 ");

Test results:
Because both the firstname and lastname controls on the Interface enter Chinese characters.
After receiving XMLHTTP. responsetext, one of them is garbled and the other can display Chinese characters normally.

On the Internet, it was also found that some people said they were using:
"The easiest way is to send all the escape messages. After retrieval, there is no encoding problem ."
After testing, I found that all things after escape changed to null. I don't know who has successfully used this method, or is there any better solution to Garbled text? Please share it with me :)
 

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.