The escape function solves the problem of garbled ajax transfer in js


This article illustrates the escape function to solve the problem of ajax transfer in js. Specific methods are as follows:

I. problem description:

Originally, escape() in web effects was url encoded according to iso-8859-1 character set in Chinese, so the request parameters could be directly obtained through request-getparameter (), but later javascript changed the escape() to unicode character set coding, so that the request parameters could not be directly obtained in JSP tutorials and servlets, for reasons I do not know.

Ii. Solutions:

1. First, escape() is encoded for Chinese characters twice. If the parameter name is to be passed and the value is “hello”, then the format of the url is… Name =escape(escape(” hello ”)) so that you can get the encoded parameter in requeste.getparameter ().

2. Since the parameter is & PI; % 25 u4f60%25 u597d   There is no way to use the regular urldecoder.decode() to decode. Fortunately, there are enough people in the world to find a tool class directly on the Internet that can implement javascript escape() and unescape() codec  

<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 The 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, and 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 The string must be. A string to be escaped or encoded.

The return value: A copy of the encoded string. Some of these characters are replaced with hexadecimal escape sequences.

Description: This method does not encode ASCII letters and Numbers, nor the following ASCII punctuation: - _.! ~ * ’(). All other characters are replaced by escape sequences.

Hints and notes: Tip: escape() encoded strings can be decoded using unescape(). Note: ecmascript v3 opposes the use of this method, instead using decodeuri() and decodeuricomponent()