Escape function Solution JS in the Ajax transfer of Chinese garbled problem _javascript skills

Source: Internet
Author: User

This article describes the escape function to solve JS in the Ajax transfer of Chinese garbled problem, to share with you for your reference. The specific methods are as follows:

First, the problem description:

In the original web effects of escape () is the Chinese in the iso-8859-1 character set URL encoding, so through the Request.getparameter () is able to get directly to the request parameters, but later JavaScript will escape () Replaced by Unicode character set encoding, this way, in the JSP tutorial and servlet can not directly get the request parameters, the specific reasons I do not know.

Ii. Solutions:

1, first of the Chinese characters for two escape () encoding, if you want to pass the parameter name, the value is "Hello", then the format of the URL is ... name=escape ("Hello"), thus, in Request.getparameter () You can get the encoded parameters.

2, because the parameters are%25u4f60%25u597d format, can not use the regular Urldecoder.decode () to decode, fortunately, the world's cattle enough, in the Internet directly found a tool class, to achieve the JavaScript escape () and unescape ()-type coding and decoding

Copy Code code 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 ("Success!") ")
}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 ("Conn_way") + "&databasename=" +escape (Get ("DatabaseName")) + "&sqlusername=" +escape ("sqlusername") + "&sqlpassword=" +escape (" SQLPassword ")) +" &sqllocalname= "+escape (" sqllocalname ") +" &pg_size= "+escape (" pg_size ") +" & Adminid= "+escape (" Adminid ") +" &adminpwd= "+escape (Get (" adminpwd "));
2727 xmlhttp.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Xmlhttp.send (Senddate)
}
</script>

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

Definition and Usage:
The escape () function encodes the string so that it can be read on all computers.
Grammar:

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

return value:
A copy of the encoded string. Some of these characters are replaced with the 16-in escape sequence.

Description
The method does not encode ASCII letters and numbers, and does not encode the following ASCII punctuation:-_. ! ~ * ' (). All other characters will be replaced by escape sequences.

Tips and Comments:
Tip: You can use Unescape () to decode an escape () encoded string.
Note: ECMAScript v3 against using this method, the application uses decodeURI () and decodeuricomponent () to replace it

I hope this article will help you with your JavaScript programming.

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.