Several background solutions for Ajax Chinese garbled characters

Source: Internet
Author: User
1. Use post to pass Parameters
 sPara += sName + "=" + encodeURI(encodeURI(sValue)) + "&"; xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open( "POST" , sURL , true ); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(sPara);


Note: Here I used encodeuri twice, encodeuri (svalue ))

String providername = request. getparameter ("providername"); // when the post is passed, it must be providername = urldecoder. Decode (providername, "UTF-8") encoded by utf8 ");

Analysis: When the request. getparameter () function is called, The URI decoding process is automatically performed. The built-in decoding process during the call may cause garbled characters. After URI encoding twice, the request. getparameter () function obtains the content encoded once by the original information Uri. You can use the controllable decoding function java.net. urldecoder. Decode () to obtain the original correct information.

2. Use get to pass Parameters
The get method is better than the POST method, but it has character restrictions.
There are two methods
1. Add the Unescape method in the background for decoding
The front-end uses the Javascript escape method for Transcoding

public static String unescape (String src){    StringBuffer tmp = new StringBuffer();    tmp.ensureCapacity(src.length());    int lastPos=0,pos=0;    char ch;    while (lastPos<src.length())    {     pos = src.indexOf("%",lastPos);     if (pos == lastPos)     {      if (src.charAt(pos+1)=='u')      {       ch = (char)Integer.parseInt(src.substring(pos+2,pos+6),16);       tmp.append(ch);       lastPos = pos+6;       }      else      {       ch = (char)Integer.parseInt(src.substring(pos+1,pos+3),16);       tmp.append(ch);       lastPos = pos+3;       }      }     else     {      if (pos == -1)      {       tmp.append(src.substring(lastPos));       lastPos=src.length();       }      else      {       tmp.append(src.substring(lastPos,pos));       lastPos=pos;       }      }    }    return tmp.toString(); }

Ii. directly use encodeuri for Transcoding
Encodeuri for transcoding at the front end
Use request. getparameter ("name"); in the background to obtain the original data.
The possible cause is that when the request. getparameter () function is called, the process will automatically perform URI decoding once

These methods are mainly used to transmit data from the front-end to the back-end. Data transferred from the back-end to the front-end is not described.


Note: This article is mainly used for the accumulation of experience, so it is not written too carefully. If someone can see it, don't scold me if you don't understand it.

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.