Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, A-Z, A-Z
Encodeuri is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, A-Z, A-Z
Encodeuricomponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, A-Z, A-Z
1.
JS: escape:
- You can use escape when JS uses data.
- For example, search for the history record in the bucket.
- When Unicode values other than 0-255 are encoded, the % u *** format is output. In other cases, escape, encodeuri, and encodeuricomponent are encoded with the same result.
2.
JS: encodeuri:
- Encodeuri can be used for URL redirection.
- Example: location. href = encodeuri ("http://cang.baidu.com/do/s? WORD = Baidu & Ct = 21 ");
3.
JS: encodeuricomponent:
- You must use encodeuricomponent when passing parameters so that the combined URL will not be truncated by special characters such.
- Example: <script language = "JavaScript"> document. Write ('<a href = "http://passport.baidu.com /? Logout & Aid = 7 & U = '+ encodeuricomponent ("http://cang.baidu.com/bruce42") +' "> exit </&
GT; '); </SCRIPT>
Ajax uses post to submit Chinese Garbled text. jsp was written some time ago. Ajax is used to submit data in post mode. If it is a Chinese character, it will be garbled, later, when I wrote ASP, I used ajax to submit data in post mode. Chinese characters are garbled. Search for the relevant information, the problem should be that the data is submitted in UTF-8 encoding, so when receiving if the use of gb2312 or other Chinese encoding will be garbled.
When using get to submit data, the Chinese problem is well solved. setRequestHeader ("Content-Type", "text/html; encoding = gb18030") is enough. However, this method does not work in the post method. We all know that the get method has a limit on the length of data submitted. Sometimes we must use the POST method to submit data.
For post, the JSP solution is as follows:
Use escape (or encodeuri, both functions are Javascript functions, and the functions are basically the same. You can check the related help), but use them twice. This is the key.
The initial page content is as follows (Hello. jsp):
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb18030" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> Ajax submission page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb18030">
<SCRIPT type = "text/JavaScript">
Function justdo (){
VaR post = "name = Wang Li Meng & Email = wallimn@sohu.com & bokee = http://wallimn.bokee.com ";
Post = encodeuri (post );
Post = encodeuri (post); // twice, critical
VaR xmlobj = new activexobject ("msxml2.xmlhttp ");
VaR url = "act. jsp"; // the file name must be changed to the corresponding location during the test.
Xmlobj. Open ("Post", URL, true );
Xmlobj. setRequestHeader ("cache-control", "No-Cache ");
Xmlobj. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ;");
Xmlobj. Send (post); // note: this is used to send content in post mode.
}
</SCRIPT>
</Head>
<Body>
<Input type = "button" value = "Submit" onclick = "justdo ()"/>
</Body>
</Html>
The encodeuri () function can encode a string as a URI and replace the character with a hexadecimal escape sequence.
The decodeuri () function can decode the URI encoded by the encodeuri () function.
Generally, the characters after the encodeuri () of JS are UTF-8 encoded. If the server-side encoding is different, iconv conversion is required.
<? PHP
$ A = urlencode (iconv ("gb2312", "UTF-8", "movie"); // equivalent to JavaScript encodeuri ("movie ");
Echo $;
$ B = iconv ("UTF-8", "gb2312", urldecode ("% E7 % 94% B5 % E5 % BD % B1 ")); // equivalent to JavaScript decodeuri ("% E7 % 94% B5 % E5 % BD % B1 ");
Echo $ B;
?>
From: http://blog.csdn.net/lichkui/article/details/2119614
Today, when extjs is used to pass parameters to the Java background, the Chinese parameters obtained by the background program are garbled and many methods have been used and cannot be solved. Baidu has written a good article, I want to help myself and my friends!
JS Code:
Function dofind (default_value ){
VaR q_bureau_name = Document. Forms [0]. q_bureau_name.value;
VaR defpars = '';
If (default_value! = NULL ){
Defpars = '& default_value =' + default_value;
}
VaR url = 'mermeroperaction. Do ';
VaR pars = 'Action = getbureaulist & sname = customertel. tel_bureau & q_bureau_name = '+ encodeuri (q_bureau_name) + defpars; // encodeuri encodes text strings into a valid Uniform Resource Identifier (URI) twice ).
VaR my = new Ajax. Request (URL, {method: 'post', parameters: pars, oncomplete: show });
}
Function show (originalrequest)
{
VaR bureauspan = Document. getelementbyid ("bureauspan ");
Bureauspan. innerhtml = originalrequest. responsetext;
}
Java code:
String q_bureau_name = util. Filter (request. getparameter ("q_bureau_name "));
Q_bureau_name = java.net. urldecoder. Decode (q_bureau_name, "UTF-8 ");
// Ajax submits data (post) in UTF-8 format by default, using the escape () or encodeuri () method provided by JavaScript. use java.net. urldecoder. decode (value, "UTF-8") method for decoding.
JS knowledge:
Escape Method
PairStringObject encoding so that they can be read on all computers,
escape(charString)
RequiredCharstringThe parameter is arbitrary to be encoded.StringObject or text.
Description
EscapeMethod returnsCharstringThe string value of the content (in unicode format ). All spaces, punctuation marks, accents, and other non-ASCII characters are used%XxEncoding, whereXxEqual to the hexadecimal number of the character. For example
"% 20 ".
If the character value is greater than 255% UXxxxFormat storage.
Note escapeThe method cannot be used to encode the Uniform Resource Identifier (URI. Encoding should be usedEncodeuriAndEncodeuricomponentMethod.
Encodeuri Method
Encodes a text string into a valid Uniform Resource Identifier (URI ).
encodeURI(URIString)
RequiredUristringThe parameter represents an encoded Uri.
Description
EncodeuriMethod returns an encoded Uri. If you pass the encoding resultDecodeuri, Then the initial string is returned.EncodeuriThe method does not encode the following characters: ":", "/", ";", and "? ". Please useEncodeuricomponentTo encode these characters.
Encodeuricomponent Method
Encodes a text string into a valid component of a uniform resource identifier (URI.
encodeURIComponent(encodedURIString)
RequiredEncodeduristringThe parameter represents an encoded URI component.
Description
EncodeuricomponentReturns an encoded Uri. If you pass the encoding resultDecodeuricomponent, Then the initial string is returned. BecauseEncodeuricomponentMethod to encode all characters. Note that if the string represents a path, for example/Folder1/folder2/default.htmlThe slash is also encoded. In this way, when the encoding result is sent as a request to the Web
The server is invalid. If the string contains more than one URI component, useEncodeuriMethod.