Chinese garbled characters encountered in jquery and Ajax Development

Source: Internet
Author: User

Chinese garbled characters may occur when values are passed through URLs in JS. The solution is as follows:

Encodeuri is used twice when the value is passed, and urldecoder. Decode is used when receiving the value in the background.

Reference code:

Backend servlet:

Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter (); try {string Param = request. getparameter ("username"); If (Param = NULL | Param. length () = 0) {out. println ("username cannot be blank");} else {string username = urldecoder. decode (Param, "UTF-8"); If (username. equals ("somnl") {out. println ("username [" + username + "] registered");} else {out. println ("You can use [" + username + "] ") ;}} finally {out. close ();}}

Front-end JS:

$ (Document ). ready (function () {$ ("# verifybutton "). click (function () {var username = $ ("# username "). val (); If (username = "") {alert ("username cannot be blank")} else {$. get ("userverify? Username = "+ encodeuri (username), null, function (response) {$ (" # result ").html (response );});}}); $ ("# username "). keyup (function (){});});

 

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.