When JavaScript calls AJAX, garbled Get and post methods have been written before, but JavaScript code is cumbersome. Let's share with you how to solve this problem using jQuery, if you encounter a similar situation, you can refer to the methods for garbled Get and post when using js to call AJAX in the Sina Blog. However, JavaScript code is cumbersome, when using ajax for data interaction, we can use jQuery, a mature framework of js.
For the design of a website, you must submit parameters to the server to obtain the required page data, whether you are registering for Logon or querying by page. To reduce the suffering caused by PAGE refreshing, ajax was born. However, beginners may encounter an annoying problem during project development: Chinese garbled characters.
Next I will use a simple example to tell you where garbled characters may occur and how to solve them.
In this example, the username is correct (already exists) during user registration. When the username text is removed from the focus, the username is asynchronously submitted and extracted and determined by the servlet, return the result to the page with a prompt.
Step 1: create a web project (in GBK format by default) named jQuery_Ajax. Create a js package in its WebRoot directory and place the jquery-1.4.4.js in it.
Step 2: Create a servlet package under src and write Vali. java
The Code is as follows:
Package servlet;
Import java. io. IOException;
Import java. io. PrintWriter;
Import java.net. URLDecoder;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Importjavax. servlet. http. HttpServletRequest;
Importjavax. servlet. http. HttpServletResponse;
Public class Vali extends HttpServlet {
@ Override
Protectedvoid service (HttpServletRequest request, HttpServletResponse response)
ThrowsServletException, IOException {
StringuserName = URLDecoder. decode (request. getParameter ("userName"), "UTF-8 ");
System. out. println (userName );
Response. setContentType ("text/html; charset = UTF-8 ");
PrintWriter pw = response. getWriter ();
If (userName. equals ("Michael ")){
Pw. println ("error ");
} Else {
Pw. println ("correct ");
}
}
}
From the code, we can see that statements containing the encoding format are one of the solutions to garbled code.
Note in the Code:
1. URLDecoder. decode (request. getParameter ("userName"), "UTF-8") -- convert and extract data from the page
2. response. setContentType ("text/html; charset = UTF-8") -- encode the response returned value in UTF-8 and return the page
3. Note that the conversion in step 2 should be written before the response of everything in this method. Otherwise, the conversion may fail.
4. This servlet only applies to the Post Method for Data Format encoding. If the submission method is GET, the code for extracting page data is as follows:
The Code is as follows:
Request. setCharacterEncoding ("UTF-8 ");
StringuserName = request. getParameter ("userName ");
UserName = new String (userName. getBytes ("iso-8859-1"), "UTF-8 ");
Step 3: Compile the simple registration page ajax. jsp
The Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
My JSP 'ajax. jsp 'starting page
Script
Function vali (){
$. Ajax ({
Type: "POST ",
Url: "/jQuery_Ajax/Vali ",
Data: encodeURI ("userName =" + $ (": text"). val ())),
Success: function (data ){
$ ("Span"). text (data );
}
});
}
Script
User name:
Password:
Note in the Code:
1. The page should be set to UTF-8 and introduce jquery-1.4.4.js
2. ajax transmits data through the POST method. Pay attention to the data settings. Enter the returned data in the span tag.
If you use the GET method to pass page data, the js Code is as follows:
The Code is as follows:
Function vali (){
$. Ajax ({
Type: "GET ",
Url: "/jQuery_Ajax/Vali ",
Data: encodeURI ("userName =" + $ (": text"). val ()),
Success: function (data ){
$ ("Span"). text (data );
}
});
}
The last step is to configure servlet and ing in web. xml.
The Code is as follows:
This is the description of my J2EEcomponent
This is the display name of my J2EEcomponent
Vali
Servlet. Vali
Vali
/Vali
After the above Code is compiled, this registration and verification project has been completed, deployed to tomcat and accessed through the web page.
Finally, we will summarize the solutions to jQuery Garbled text.:
1. Check the page encoding and set the page encoding to utf8 as follows:
2. Check the servlet and add the following code to the doPost or doGet method:
Response. setContentType ("text/xml; charset = UTF-8 ");
3. Modify the tomcat file and add URIEncoding = "utf8" to the TOMCAT_HOME/conf/server. xml file ":
4. Add a filter in the project and set the encoding method to utf8.
After the above four steps, the problem persists.
5. Check the http header of ie and view the contentType field as follows:
ContentType: "application/x-www-form-urlencoded"
6. Check the http header of firefox and view the contentType field as follows:
ContentType: "application/x-www-form-urlencoded; charset = UTF-8"
Compared with Step 5 and step 6, the problem occurs.
7. Modify the jQuery-1.x.x.js file
ContentType: "application/x-www-form-urlencoded" changed to the following code:
ContentType: "application/x-www-form-urlencoded; charset = UTF-8"