Solve the problem of Chinese garbled when Ajax submits form

Source: Internet
Author: User
Tags date functions key net string

Online search combined with the situation encountered in their own development process, to prepare for forgetting:

Ajax form submission data garbled and solutions

The parameters to pass
var querystring = "Firstname=" + firstName + "&lastname=" + lastName
+ "&birthday=" + birthday; function

  1. Get method Submit
  2. Dorequestusingget () {
  3. Createxmlhttprequest ();
  4. var url = "Getandpostexample?" + querystring + "&timestamp="
  5. + New Date (). GetTime ();
  6. Xmlhttp.onreadystatechange = Handlestatechange;
  7. Xmlhttp.open ("Get", url, True);
  8. Xmlhttp.send (NULL);
  9. }
  10. Post method submission
  11. function Dorequestusingpost () {
  12. Createxmlhttprequest ();
  13. var url = "getandpostexample?timestamp=" + New Date (). GetTime ();
  14. Xmlhttp.open ("POST", url, True);
  15. Xmlhttp.onreadystatechange = Handlestatechange;
  16. Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
  17. Xmlhttp.send (querystring);
  18. }
When you receive parameters in the servlet, you must://When using the Post method, must be set to Utf-8, otherwise garbled
String firstName = new String (Request.getparameter ("FirstName"). GetBytes ("Iso-8859-1"), "Utf-8");
// when using get method, want to set to GB2312, otherwise garbled。
String lastName = new String (Request.getparameter ("LastName"). GetBytes ("Iso-8859-1"), "GB2312");

In another netizen Autumn memory blog , found another way, the following is the original content:

Some time ago to write JSP, using AJAX to post the submission of data, if it is a Chinese character submission will be garbled, and then used to write ASP Ajax to post way to submit data, Chinese is garbled. Search for relevant information, the problem should be submitted to the data is UTF-8 encoding, so when the receiver if the use of GB2312 or other Chinese code will be garbled.

When using get method to submit data, Chinese problem is solved very well, setrequestheader ("Content-type", "text/html; encoding=gb18030") is OK. However, this method does not work in post mode. We all know that the get-way submission data has a length limit, and sometimes we have to submit the data using the Post method.

for using post,the solution for JSP is as follows:
Use Escape (or encodeURI, two functions are JavaScript functions, basically the same function, you can check the relevant help), but to use two times, this is the key.

The initial page content is as follows (hello.jsp):
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>ajax Submit Page </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<script type= "Text/javascript" >
function Justdo () {
var post = "Name= Wang Li-Mun &email=wallimn@sohu.com&bokee=http://wallimn.bokee.com";
Post =encodeURI(post);
Post =encodeURI(post); Two times, that's the key.
var xmlobj = new ActiveXObject ("Msxml2.xmlhttp");
var URL = "act.jsp"; The filename needs to be adjusted to the appropriate location at test time
Xmlobj.open ("POST", url,true);
Xmlobj.setrequestheader ("Cache-control", "No-cache");
Xmlobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded;");
Xmlobj.send (POST); Note: Post way, use this to send content
}
</script>
<body>
<input type= "button" value= "Submit"/>
</body>



Ajax Request Processing page (act.jsp) reads as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<% @page import= "Java.net.URLDecoder"%>
<title>ajax deal</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<body>
<%
Traverses the output parameter contents.
For (Enumeration E = Request.getparameternames (); e.hasmoreelements ();) {
String h = (string) e.nextelement ();
String v = request.getparameter (h);
String mm = Java.net.URLDecoder.decode(V, "UTF-8");
SYSTEM.OUT.PRINTLN ("Request parameter:" + H + "=" + mm);
}
%>
</body>


Analysis: When the Request.getparameter () function is invoked, the decoding process of the URI is automatically performed, and the decoding process built into the call will cause garbled characters to appear. After the URI is encoded two times, the Request.getparameter () function gets the content that the original information URI encodes once. The Java.net.URLDecoder.decode () can be used to solve the original correct information by using the controllable decoding function.


for ASPs, you can use JavaScript's encodeuricomponent function on the client (other encoding functions may be OK, no test) encoded two times, The ASP then decodes the decodeuricomponent using JavaScript after receiving the Request.Form:

The initial page content is as follows (hello.asp):
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>ajax Submit Page </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<script type= "Text/javascript" >
function Justdo () {
var post = "Name=" +encodeuricomponent( encodeuricomponent("Wang Li-Mun")); //two times
var xmlobj = new ActiveXObject ("Msxml2.xmlhttp");
var URL = "act.asp"; The filename needs to be adjusted to the appropriate location at test time
Xmlobj.open ("POST", url,true);
Xmlobj.setrequestheader ("Cache-control", "No-cache");
Xmlobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded;");
Xmlobj.send (POST); Note: Post way, use this to send content
}
</script>
<body>
<input type= "button" value= "Submit"/>
</body>


Ajax Request Processing page (act.asp) reads as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>ajax deal</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<body>
<%
Dim context
context = Decodetext(Request.Form ("name"))
Response.Write ("Name=" & Context)
%>
</body>

<script language= "javascript" runat= "Server" >
function Decodetext (str) {
return (str = null?) "": decodeURIComponent (str));
}
</script>



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.