Garbled data is submitted during Ajax development.

Source: Internet
Author: User

UTF-8 transcoding can be used in the background to solve Chinese garbled characters. This method is applicable to form submission and encoding is completed on the server. The Code is as follows:

Form. jsp:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> test form submission </title>
<Script language = "JavaScript">
VaR http_request = false;
Function send_request (URL, poststr) {// initialize, specify the processing function, and send the request Function
Http_request = false;
// Start initializing the XMLHTTPRequest object

 
If (window. XMLHttpRequest) {// Mozilla Browser
Http_request = new XMLHttpRequest ();
If (http_request.overridemimetype) {// sets the mime category
Http_request.overridemimetype ('text/xml ');
}
}
Else if (window. activexobject) {// IE browser
Try {
Http_request = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Http_request = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
If (! Http_request) {// exception. An error occurred while creating the object instance.
Window. Alert ("the XMLHTTPRequest object instance cannot be created .");
Return false;
}
Http_request.onreadystatechange = processrequest;
// Determine the request sending method and URL and whether to execute the following code synchronously
Http_request.open ("Post", URL, true );
Http_request.setrequestheader ("Content-Type", "application/X-WWW-form-urlencoded ");
Http_request.send (poststr );
}
// Function for processing the returned information
Function processrequest (){
If (http_request.readystate = 4) {// judge the object status
If (http_request.status = 200) {// The information has been returned successfully. Start to process the information.
Alert (http_request.responsetext );
} Else {// The page is abnormal.
Alert ("the page you requested has an exception. ");
}
}
}
Function form_submit (){
VaR F = Document. form1;
VaR username = f. username. value;
VaR Password = f. Password. value;
VaR poststr = "";
If (username = ""){
Window. Alert ("the user name cannot be blank. ");
F. username. Focus ();
Return false;
}
If (Password = ""){
Window. Alert ("the password cannot be blank. ");
F. Password. Focus ();
Return false;
}
Poststr = "username =" + encodeuri (F. username. Value) + "& Password =" + F. Password. value;
Send_request ('form _ HANDLE. jsp ', poststr );
Return false;
}
</SCRIPT>
</Head>

<Body>
<Form name = "form1" method = "Post" Action = "" onsubmit = "Return form_submit ()">
<Table width = "300" border = "0" cellspacing = "4" cellpadding = "0" style = "font-size: 12pt;">
<Tr>
<TD width = "74" Height = "25"> User name: </TD>
<TD width = "220" Height = "25"> <input name = "username" type = "text" id = "username" size = "20"> </TD>
</Tr>
<Tr>
<TD Height = "25"> password: </TD>
<TD Height = "25"> <input name = "password" type = "password" id = "password" size = "20"> </TD>
</Tr>
<Tr align = "center">
<TD Height = "25" colspan = "2"> <input type = "Submit" name = "Submit" value = "Submit"> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

Form_handle.jsp:
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" errorpage = "" %>
<%
String username = new string (request. getparameter ("username"). getbytes ("ISO-8859-1"), "utf8 ");
String Password = request. getparameter ("password ");
System. Out. println ("username:" + username );
System. Out. println ("Password:" + password );
Out. println (username + "|" + password );
%>

 

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.