This article describes the escape function solving the problem of garbled code in Ajax in JS., and it is shared with you for your reference. The specific methods are as follows:
1. Problem description:
Originally, the escape() in the web page special effects url coded in Chinese according to the iso-8859-1 character set. In this way, the request parameters can be directly obtained through request.getparameter(), but later JavaScript replaced escape() with unicode character set encoding. In this way, the request parameters cannot be obtained directly in the jsp tutorial and servlet, and I don’t know the specific reason.
2. Solution:
1. First, encode the Chinese characters twice. If you want to pass the parameter name and the value is "Hello", the format of the url is....name=escape(escape("Hello")). In this way, the encoded parameters can be obtained in request.getparameter().
2. Since the parameters obtained are in the format of %25u4f60%25u597d, it is impossible to use the regular urldecoder.decode() to decode. Fortunately, there are enough people in this world. I directly found a tool class on the Internet that can implement escape() and unescape()-style codec in JavaScript.
Copy the 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(get("conn_way"))+"&databasename="+escape(get("databasename"))+"&sqlusername="+escape(get("sqlusername"))+"&sqlpassword="+esc ape(get("sqlpassword"))+"&sqlllocalname="+escape(get("sqlllocalname"))+"&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 example above, we only use the escape function in Chinese, and the syntax is as follows:
Definition and usage:
The escape() function encodes a string so that the string 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 hexadecimal escape sequences.
illustrate:
This method does not encode ascii letters and numbers, nor does it encode the following ascii punctuation marks: - _ . ! ~ * ' ( ) . All other characters will be replaced by escaped sequences.
Tips and comments:
Tip: Unescape() can be used to decode escape() encoded strings.
Note: ecmascript v3 opposes the use of this method, and the application uses decodeuri() and decodeuricomponent() instead of it
I hope this article will be helpful to everyone's JavaScript programming.