Ajax Post submit data program accepts Chinese garbled solution

Source: Internet
Author: User
Tags reflection

Ajax to post, the URL in the form of "url?param=xx&param2=xx", if the parameters are passed in Chinese or Japanese, background to receive parameters will appear garbled, suspected "Hao? Innocent?" ㄨ?? rdquo;. At this point we need to encode the URL into UTF-8. Invokes the JavaScript method encodeURI (uristring), and the background receive parameter calls Urldecoder.decode (String s,string Enc) decoding.
Work encountered this problem, initially intended to use reflection loop to the property transcoding, considering the problem of reflection efficiency, in order to avoid abuse of reflection. Then think of the Beanutils Toolkit can be customized before the operation? It's a terrific. The following is an example of a work encounter in which the page serializes all forms into a URL parameter form string through jquery, and commits it via Ajax. The Formbean in the background struts package gets all the parameter values and invokes the Beanutils Toolkit Copyproperties (Object dest, Object Orig) method to copy Formbean to Valuebean. Before copying the object properties, customize the string encoding converter, which is Urldecoder.decode (String s,string Enc) to convert value. All properties of the object will be filtered into Chinese by the custom converter.

The solution to this problem is very simple, only need to use the escape () function in the JS processing parameters can be, and do not need to use unescape () decoding, this method is suitable for post and get way, specific AJAX code here I do not, here give out with Escape () Code when the function processes parameters:

The code is as follows Copy Code
var htmer = "getcode=" +escape (getcode) + "&content=" +escape (Content);

Usually when we are dealing with Ajax, we will get the value of the parameters directly here, in order to make the Chinese parameter is not garbled, we only need to use the escape () function to deal with the parameters.

Question Two

When the website page is not utf-8 encoded, the Chinese that the Ajax submits will become garbled, the solution is as follows:

Find the Jquery.js.

The code is as follows Copy Code


contenttype:application/x-www-form-urlencoded

Change it to


contenttype:application/x-www-form-urlencoded; Charset=utf-8

It's OK. Reason: When CharSet is not specified, jquery uses iso-8859-1,iso8859-1, often called Latin-1. Latin-1 includes additional characters that are indispensable for writing all Western European languages. jquery Ajax does not take into account the internationalization of the problem, and the use of European character sets, so the transfer of Chinese will only appear garbled.

Example

JS Ajax

The code is as follows Copy Code

if (confirm) (OK to save?) ")){
var url = "Xxaction.do?method=xxmethod";
var data = $ ("Form:first"). Serialize ();
data = encodeURI (data); The submit data URL is encoded by encodeURI (uristring)
$.ajax ({
Type: "POST",
Url:url,
Data:data,
Success:function (msg) {
Alert (msg);
}
});
}

Background program

  code is as follows copy code

private void Formbeantovaluebean (Zmxgzzmsyvb vb, ZMXGZZMSYFB fb)
  throws Exception {
 // Custom conversion string Encoding Converter
 convertutils.register (new Converter () {
  public Object convert (Class type) Object Value {
   if (value = = null) {
    return null;
  & nbsp;} else {
    string val = ';
    try {
     //is decoded by Urldecoder.decode (String s,String Enc)
     val = Urldecoder.decode (value.tostring (), "UTF-8");
    } catch (Exception e) {
     e.printstacktrace ();
   &NBSP}
    return val;
   
  }
 }, String.class);
 beanutils.copyproperties (VB, FB);
 convertutils.deregister ();
}

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.