Encoding problem of transferring Chinese parameters in Ajax

Source: Internet
Author: User
Tags return string split stringbuffer
Ajax| Code | questions | chinese

Problem encountered: When using XHR to issue a request, the requested URL in the parameter with Chinese, then in the background parsing request can not be Chinese
Such as:
Request = new ActiveXObject ("Microsoft.XMLHTTP");
Request.open ("get", url); My URL needs to have Chinese parameters
The direct plus parameter is not resolved in the background, so I use the method to encode the URL to convert

Transfer code before the Chinese parameter is passed in the page:


function EnCode (CHINESESTR) {
Return Escape (chinesestr, ' utf-8 ');
}

To generate a URL using a string that has been turned into a horse

And then we're going to decode it backstage.
Think you can not directly from request to get the encoded string, so use the method to remove the URL, and then remove the parameters of the URL to decode
Remove URL, and take parameter method

String xxx= Geturlparameter (unescape (request.getquerystring ()), "XXX");

XXX So obtained is the correct Chinese after decoding



public string Geturlparameter (string str,string targetstr) {

string[] Stringarray = Str.split ("&");

for (int i=0;i<stringarray.length;i++) {
if (Stringarray[i].startswith (TARGETSTR)) {
Stringarray=stringarray[i].split ("=");
Break
}
}
return stringarray[1];
}

Decoding method
The following is the decoding of JS escape
Private final static byte[] val = {0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x01,
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x0A, 0x0b, 0x0C, 0x0D, 0x0e, 0x0f, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 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, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F};


/**
* Decoding instructions: This method ensures that the correct "decoding" result can be obtained regardless of whether the parameter S is encoded by Escape ().
*
* @param s
* @return
*/
public static string unescape (string s) {
StringBuffer sbuf = new StringBuffer ();
int i = 0;
int len = S.length ();
while (I < Len) {
int ch = S.charat (i);
if (' A ' <= ch && ch <= ' Z ') {//' a ' ... ' Z ': as it was
Sbuf.append ((char) ch);
else if (' A ' <= ch && ch <= ' z ') {//' a ' ... ') Z ': as it was
Sbuf.append ((char) ch);
else if (' 0 ' <= ch && ch <= ' 9 ') {//' 0 ' ... ') 9 ': as it was
Sbuf.append ((char) ch);
else if (ch = = '-' | | ch = = ' _ '//Unreserved:as it was
|| ch = = '. ' | | CH = = '! ' | | ch = = ' ~ ' | | CH = = ' * '
|| ch = = ' \ ' | | ch = = ' (' | | | ch = = ') ') {
Sbuf.append ((char) ch);
else if (ch = = '% ') {
int cint = 0;
if (' U '!= s.charat (i + 1)) {//%xx:map to ASCII (XX)
CInt = (CInt << 4) | Val[s.charat (i + 1)];
CInt = (CInt << 4) | Val[s.charat (i + 2)];
i + 2;
else {//%uxxxx:map to Unicode (XXXX)
CInt = (CInt << 4) | Val[s.charat (i + 2)];
CInt = (CInt << 4) | Val[s.charat (i + 3)];
CInt = (CInt << 4) | Val[s.charat (i + 4)];
CInt = (CInt << 4) | Val[s.charat (i + 5)];
i + 5;
}
Sbuf.append ((char) cint);
else {//the corresponding character is not encoded
Sbuf.append ((char) ch);
}
i++;
}
return sbuf.tostring ();
}


The Exfirstword is the correct Chinese.
Problem solving

When a request is made using XHR, an HTTP package is actually sent to the background, except that the HTTP package is encapsulated XMLHttpRequest, and XMLHttpRequest is encoded in Chinese gb2312 so that because the page is UTF-8 encoded, So there will be problems in the background.
So if your page is GBK or gb2312 encoded, you're not going to have a lot of trouble with XMLHttpRequest.



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.