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 (){});});