对要发送到web服务器的查询字符串进行编码时-何时使用escape(),何时使用encodeURI()或encodeURIComponent():

使用转义符:

escape("% +&=");

OR

使用encodeURI()/encodeURIComponent()

encodeURI("http://www.google.com?var1=value1&var2=value2");

encodeURIComponent("var1=value1&var2=value2");

当前回答

@johann echavarria答案的现代改写:

控制台日志(阵列(256).fill().map((忽略,i)=>String.fromCharCode(i)).过滤器((字符)=>encodeURI(char)!==encodeURIComponent(字符)? {字符:char,encodeURI:encodeURI(char),encodeURIComponent:encodeURIComponent(char)}:错误))

或者,如果可以使用表,请将console.log替换为console.table(以获得更漂亮的输出)。

其他回答

我有这个功能。。。

var escapeURIparam = function(url) {
    if (encodeURIComponent) url = encodeURIComponent(url);
    else if (encodeURI) url = encodeURI(url);
    else url = escape(url);
    url = url.replace(/\+/g, '%2B'); // Force the replacement of "+"
    return url;
};

我发现,即使在很好地掌握了各种方法的用途和功能之后,尝试各种方法也是一种很好的理智检查。

为此,我发现这个网站非常有用,可以证实我的怀疑,即我正在做一些适当的事情。它还被证明对解码encodeURIComponented字符串非常有用,这对解释来说可能相当困难。一个很棒的书签:

http://www.the-art-of-web.com/javascript/escape/

encodeURIComponent不编码-__!~*'(),导致以xml字符串将数据发布到php时出现问题。

例如:<xml><text x=“100”y=“150”value=“这是一个带单引号的值”/></xml>

带encodeURI的常规转义%3Cxml%3E%3Text%20x=%22100%22%20y=%22150%22%20%20value=%2它是%20a%20value%20,带有%20single%20quote%22%20/%3E%20%3C/xml%3E

您可以看到,单引号没有编码。为了解决问题,我创建了两个函数来解决项目中的问题,即编码URL:

function encodeData(s:String):String{
    return encodeURIComponent(s).replace(/\-/g, "%2D").replace(/\_/g, "%5F").replace(/\./g, "%2E").replace(/\!/g, "%21").replace(/\~/g, "%7E").replace(/\*/g, "%2A").replace(/\'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
}

对于解码URL:

function decodeData(s:String):String{
    try{
        return decodeURIComponent(s.replace(/\%2D/g, "-").replace(/\%5F/g, "_").replace(/\%2E/g, ".").replace(/\%21/g, "!").replace(/\%7E/g, "~").replace(/\%2A/g, "*").replace(/\%27/g, "'").replace(/\%28/g, "(").replace(/\%29/g, ")"));
    }catch (e:Error) {
    }
    return "";
}

Java与JavaScript与PHP的小比较表。

1. Java URLEncoder.encode (using UTF8 charset)
2. JavaScript encodeURIComponent
3. JavaScript escape
4. PHP urlencode
5. PHP rawurlencode

char   JAVA JavaScript --PHP---
[ ]     +    %20  %20  +    %20
[!]     %21  !    %21  %21  %21
[*]     *    *    *    %2A  %2A
[']     %27  '    %27  %27  %27 
[(]     %28  (    %28  %28  %28
[)]     %29  )    %29  %29  %29
[;]     %3B  %3B  %3B  %3B  %3B
[:]     %3A  %3A  %3A  %3A  %3A
[@]     %40  %40  @    %40  %40
[&]     %26  %26  %26  %26  %26
[=]     %3D  %3D  %3D  %3D  %3D
[+]     %2B  %2B  +    %2B  %2B
[$]     %24  %24  %24  %24  %24
[,]     %2C  %2C  %2C  %2C  %2C
[/]     %2F  %2F  /    %2F  %2F
[?]     %3F  %3F  %3F  %3F  %3F
[#]     %23  %23  %23  %23  %23
[[]     %5B  %5B  %5B  %5B  %5B
[]]     %5D  %5D  %5D  %5D  %5D
----------------------------------------
[~]     %7E  ~    %7E  %7E  ~
[-]     -    -    -    -    -
[_]     _    _    _    _    _
[%]     %25  %25  %25  %25  %25
[\]     %5C  %5C  %5C  %5C  %5C
----------------------------------------
char  -JAVA-  --JavaScript--  -----PHP------
[ä]   %C3%A4  %C3%A4  %E4     %C3%A4  %C3%A4
[ф]   %D1%84  %D1%84  %u0444  %D1%84  %D1%84

只需自己尝试encodeURI()和encodeURIComponent()。。。

console.log(encodeURIComponent('@#$%^&*'));

输入:@#$%^&*。输出:%40%23%24%25%5E%26*。等等,你怎么了?为什么没有转换?如果您尝试执行linux命令“$string”,这肯定会导致问题。TLDR:您实际上需要fixedEncodeURIComponent()和fixedEncode URI()。长话短说。。。

何时使用encodeURI()?从不encodeURI()在括号编码方面未能遵守RFC3986。按照MDN encodeURI()文档中的定义和进一步解释,使用fixedEncodeURI(。。。

函数fixedEncodeURI(str){return encodeURI(str).replace(/%5B/g,'[').replace(/%5D/g,']');}

何时使用encodeURIComponent()?从不encodeURIComponent()在编码方面未能遵守RFC3986:!'()*. 按照MDN encodeURIComponent()文档中的定义和进一步解释,使用fixedEncodeURIComponents()。。。

函数fixedEncodeURIComponent(str){return encodeURIComponent(str).replace(/[!'()*]/g,函数(c){return“%”+c.charCodeAt(0).toString(16);});}

然后,您可以使用fixedEncodeURI()对单个URL片段进行编码,而fixedEncode URIComponent()将对URL片段和连接器进行编码;或者,简单地说,fixedEncodeURI()不会编码+@?=:#;,$&(因为&和+是常见的URL运算符),但fixedEncodeURIComponent()会。