Detailed explanation of encodeURIComponent (URL) encoding and decodeURIComponent (URL) decoding in JS

(1) encodeURIComponent(url) function

Definition and Usage 

      The encodeURIComponent() function encodes a string as a URI component.

Syntax 
      encodeURIComponent(URIstring)

Parameter Description 

      URIstring Required. A string containing the URI component or other text to encode.

return value 

      A copy of the URIstring with some characters replaced by hexadecimal escape sequences.

illustrate 

      This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks: - _ . ! ~ * ' ( ) .

      Other characters (eg: ;/?:@&=+$,# these are the punctuation marks used to separate URI components), which are separated by one or more hexadecimal escape sequences

column replacement.

(1) decodeURIComponent(url) function

Definition and Usage 

      The decodeURIComponent() function decodes a string as a URI component .

grammar 

    decodeURIComponent(URIstring)

Parameter Description 

     URIstring Required. A string containing the URI component or other text to decode.

return value 

      A copy of the URIstring with certain characters converted to the corresponding ACSII characters by hexadecimal escape sequences.

      Today, when using the jQuery post of js to pass the concatenated string data to the background servlet, some special characters such as "+, @, #,

$, %, &, ?, /" etc. cannot be passed to background output.

for example:

       var str1="a+aa+bb@kk$dd";

       var  data="data1"="+str1+"+"&"+"data2"+str2;

         If it is not encoded, passing data to the background through js's jQuery post or using window.self.location will cause +,

@, $ characters cannot be output normally.

Correct spelling:

        var str1=encodeURIComponent("a+aa+bb@kk$dd");

        var  data="data1"="+str1+"+"&"+"data2"+str2;


       When using window.self.location to pass data to the background, there is a special case, which is the decoding method in java:

The problem that JavaScript cannot be decoded in the background after encoding with encodeURIComponent.

Currently written:

      window.self.location="index.jsp?data="+encodeURIComponent(url);

The code processed by java is:

      searchtext=java.net.URLDecoder.decode(searchtext,"UTF-8");

          At first glance, it seems that there is no problem, and it should be ok with one compilation and one solution. But still garbled. Reason
 
:

It turns out that when assigning data        in the background java program , it has already used a decoding, but the decoding result is still wrong. therefore we

可以在页面上进行两次编码操作,这样后台自动的那次就可以抵消掉一次,然后在使用

data=java.net.URLDecoder.decode(data,"UTF-8");进行一次解码就好了。


正确的代码:

JavaScript:

         window.self.location="index.jsp?data="+encodeURIComponent(encodeURIComponent(url));
java:

        searchtext=java.net.URLDecoder.decode(data,"UTF-8");

        另外还有一种方法是JavaScript进行一次编码,后台java处理时换种想法就好了:

java代码:

         String s = new String(request.getParameter("data").getBytes("ISO8859-1"), "UTF-8");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906821&siteId=291194637