Get and post public garbled characters when jQuery calls AJAX

Source: Internet
Author: User

In the past, I wrote about the garbled solution of Get and post when js calls AJAX in Sina Blog, but it is complicated to use js Code, 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. the java 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 r Equest, 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 ("James") {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 the data from the page. response. setContentType ("text/html; charset = UTF-8") -- encode the response returned value in UTF-8 and return to 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. 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: copy the Code as follows: request. setCharacterEncoding ("UTF-8"); StringuserName = request. getParameter ("userName"); userName = new String (userName. getBytes ("iso-885 9-1 ")," UTF-8 "); Step 3: Compile the simple registration page ajax. the jsp 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 + "/"; %> <! Doctype html public "-// W3C // DTDHTML 4.01 Transitional // EN">

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.