Ajax passed the value of the UTF-8 encoding format, the client text to the server side, if the server code format or the use of the MVC Framework encoding format is not UTF-8, it is likely to appear in Chinese garbled. The solution is as follows:
The client uses the JS function encodeURI () to encode the Chinese characters two times, and the server side uses the Urldecoder class to decode the UTF-8 format of the Chinese characters transmitted by the client. Example:
$.ajax ({
type: "Post",
URL: "Createnewgroup.action",
data: "Name=" +encodeuri (encodeURI ("John")),
Success:function (msg) {
alert (msg);
}
});
Server-side code:
String name = Urldecoder.decode ("Client-transmitted Chinese characters", "UTF-8");
When the characters in the server end to the client transmission are garbled, the server can encode the UTF-8 format with the Urlencoder class. The client uses the JS function decodeURI () to decode the medium characters transmitted by the server side two times.
When the server side transmits a JSON string, it is not possible to encode the entire JSON string in UTF-8 format (the encoded JSON string may no longer have JSON's original format). The value in the JSON string can be encoded separately using the Jsonvalueprocessor interface and the Jsonconfig class.
Sample code:
Jsonconfig jsonconfig = new Jsonconfig (); Jsonconfig.registerjsonvalueprocessor (String.class, New Jsonvalueprocessor () {public Object processarrayvalue (
Object value, Jsonconfig jsonconfig) {return process (value);}
Public Object Processobjectvalue (String key, Object value, Jsonconfig jsonconfig) {return process (value);} /** * Process * @param value * @return/Public Object process (object value) {try {if (value instanceof String) {Retu
RN Urlencoder.encode (value.tostring (), "UTF-8");
return value = = null? ": Value.tostring ();
catch (Exception e) {return "";}}}); Jsonarray json = Jsonarray.fromobject ("[{name:\" john \ "; age:\12\"; Sex:\ "men \"}] ", jsonconfig);
Encoded JSON string [Java] View plaincopyprint?
Jsonconfig jsonconfig = new Jsonconfig (); Jsonconfig.registerjsonvalueprocessor (String.class, New Jsonvalueprocessor () {public Object Processarray
Value (Object value, Jsonconfig jsonconfig) {return process (value);
} Public Object Processobjectvalue (String key, Object value, Jsonconfig jsonconfig) {RET
Urn process (value); /** * Process * @param value * @return/P
Ublic Object Process (object value) {try {if (value instanceof String) {
Return Urlencoder.encode (Value.tostring (), "UTF-8"); return value = = null?
"": value.tostring ();
catch (Exception e) {return "";
}
}
});
Jsonarray json = Jsonarray.fromobject ("[{name:\" john \ "; age:\12\"; Sex:\ "men \"}] ", Jsonconfig ); Encoded JSON string
The client uses the function decodeURI () to decode the value in the JSON string two times.
Using the XMLHttpRequest object on the browser side to send Chinese parameters to the server, if not in the browser and server-side processing, there will be Chinese garbled problem. There are many solutions to this problem, but it is often the case that Chinese can be displayed normally under IE, in other browsers ( Firefox, for example, is still garbled. Under Firefox to solve the garbled problem, but returned to IE under the access but garbled problem. So in the current popular browser to solve the problem of garbled Chinese, can make the Web program to become generic.
The solution to this problem, I summarize roughly two kinds:
First, the Chinese parameters to be passed are encoded at the browser end. The code is as follows:
Xmlhttp.open ("POST", "Ajaxservlet", true); Request parameter Initialization
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Because the request is post, you set the request header here. (This code can be omitted if the request is a get.)
Xmlhttp.send ("Name=" +encodeuri) (encodeURI ("China")); Send parameters to server side
On the server-side code:
PrintWriter out = Response.getwriter (); Get the output stream object of response
String name1 = request.getparameter ("name"); Get the request parameter with key "name"
String name = Urldecoder.decode (name1, "utf-8"); Decoding the resulting parameters
Out.print (name); Sending data to the browser end
Second, the Chinese parameters to be passed are encoded at the browser end. The code is as follows:
Xmlhttp.open ("POST", "Ajaxservlet", true); Request parameter Initialization
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Because the request is post, you set the request header here. (This code can be omitted if the request is a get.)
Xmlhttp.send ("Name=" +encodeuri ("China")); Send parameters to server side
On the server-side code:
PrintWriter out = Response.getwriter (); Get the output stream object of response
String name1 = request.getparameter ("name"); Get the request parameter with key "name"
String name = new String ((Name1.getbytes ("iso-8859-1")), "UTF-8"); Decoding the resulting parameters
Out.print (name); Sending data to the browser end
The above two methods, when using the XMLHttpRequest object to pass Chinese parameters, garbled problem can be solved under any browser, the correct display of Chinese.
The original source has not remembered, hope the author can forgive,