The escape Function solves the problem of garbled characters in ajax transmission in js.

Source: Internet
Author: User

The escape Function solves the problem of garbled characters in ajax transmission in js.

The example in this article describes how the escape Function solves the problem of garbled characters in ajax transmission in js, and shares it with you for your reference. The specific method is as follows:

I. Problem description:

Originally in the Web Effect of escape () is the Chinese according to the iso-8859-1 Character Set url encoding, that through request. getparameter () can directly obtain the request parameters, but later javascript will replace escape () with the unicode Character Set encoding, so that, in the jsp tutorial and servlet, the request parameters cannot be obtained directly. I do not know the specific reason.

Ii. solution:

1. encode the Chinese characters twice. If you want to pass the parameter name and the value is "hello", the url format is .... name = escape ("hello. getparameter () can get the encoded parameter.

2. Because the obtained parameter is in the format of % 25u4f60% 25u597d, the standard urldecoder cannot be used. decode () to decode. Fortunately, there are enough coders in the world. I have found a tool class on the Internet to implement escape () and unescape () encoding and decoding in javascript.
 
Copy codeThe Code is as follows: <script language = "javascript">
Function get (id) {return document. getelementbyid (id). value}
Function setting ()
{
Var xmlhttp;
If (window. activexobject)
{
Xmlhttp = new activexobject ("microsoft. xmlhttp ")
} Else {
Xmlhttp = new xmlhttprequest ();
}
Xmlhttp. onreadystatechange = function ()
{
If (xmlhttp. readystate = 4)
{
If (xmlhttp. status = 200)
{
Alert ("successful! ")
} Else {
Alert (xmlhttp. status)
}
}
}
Var url = "action. asp tutorial? Action = setting & rnd = "+ math. random ()
Xmlhttp. open ("post", url, true)
Var senddate = "title =" + escape (get ("title") + "& conn_way =" + escape (get ("conn_way ")) + "& databasename =" + escape (get ("databasename") + "& sqlusername =" + escape (get ("sqlusername ")) + "& sqlpassword =" + escape (get ("sqlpassword") + "& sqllocalname =" + escape (get ("sqllocalname ")) + "& pg_size =" + escape (get ("pg_size") + "& adminid =" + escape (get ("adminid ")) + "& adminpwd =" + escape (get ("adminpwd "));
2727 xmlhttp. setrequestheader ('content-type', 'application/x-www-form-urlencoded ');
Xmlhttp. send (senddate)
}
</Script>

In the above example, we only use the escape Function in Chinese. The syntax is as follows:

Definition and usage:
The escape () function can encode a string so that the string can be read on all computers.
Syntax:

Escape (string) parameter description
String is required. The string to be escaped or encoded.

Return Value:
A copy of the encoded string. Some characters are replaced with hexadecimal escape sequences.

Note:
This method does not encode ascii letters and numbers or the following ascii punctuation marks :-_.! ~ *'(). All other characters are replaced by escape sequences.

Tips and notes:
Tip: You can use unescape () to decode the escape () encoded string.
Note: ecmascript v3 rejects this method. The application uses decodeuri () and decodeuricomponent () to replace it.

I hope this article will help you design javascript programs.


How can I pass the Chinese parameters of JavaScript ajax correctly to JavaScript without garbled characters?

Modify the configuration in server. xml in the path \ Tomcat 6.0 \ conf,
<Connector port = "8080" protocol = "HTTP/1.1"
ConnectionTimeout = "20000"
RedirectPort = "8443" URIEncoding = "UTF-8"/>, mainly to add URIEncoding = "UTF-8"
 
Jsp uses nested ajax in js functions to pass parameters to servlet. Chinese garbled characters (question marks) cannot be solved by searching the entire network.

Reference answer: This term is used to summarize a typical music style Between 1600 and 1750. In the 17th century, when philosophers and scientists began to abandon the old set of ideas and methods about the world and establish a more effective and Logical Understanding basis, musicians are also seeking to express new emotions and spiritual realm with new music languages, trying, more intense emotional content is integrated into the form of music inherited from the Renaissance. Until the middle of the 17th century, new music languages and styles were gradually formed, such as the extended music, the dynamic rhythm, strong comparison, and music sentiment conversion, this makes music forms develop in all aspects that have never been used before.

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.