为什么没有连接codeURIComponent EN code的单层引号/撇号? [英] Why doesn't encodeURIComponent encode sinlge quotes/apostrophes?

查看:115
本文介绍了为什么没有连接codeURIComponent EN code的单层引号/撇号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

越狱()的功能,被德precated,取而代之的连接codeURIComponent ,但连接codeURIComponent 没有连接code单引号/撇号字符。我需要为躲避一个人的姓氏(例如,奥尼尔)撇号在AJAX的形式。他们为什么要删除的东西,他们正在努力改善的能力?

编辑:

因此​​,这里是一个code例子来更彻底地说明这个问题。因此,大家可以看到姓'奥尼尔'包含需要传递的URL中的变量时进行转义撇号。但是,这也发生在其他地方的形式,例如,如果一个地址输入的是比利的小酒馆。

 <输入ID ='姓'值=奥尼尔>
<脚本>
VAR get_url = + EN codeURIComponent($('#姓)VAL()。)姓=?;
$阿贾克斯({
    网址:get_url
});
< / SCRIPT>
 

我目前的解决方案,使用自定义功能。我的问题只是要问,为什么还需要一个自定义的功能。

 <脚本>
功能customEn codeURIComponent(URI){
    返回EN codeURIComponent(URI).replace(/'/克,%27);
}
< / SCRIPT>

<输入ID ='姓'值=奥尼尔>
<脚本>
VAR get_url = + customEn codeURIComponent($('#姓)VAL()。)姓=?;
$阿贾克斯({
    网址:get_url
});
< / SCRIPT>
 

解决方案

连接codeURIComponent 转义的所有字符,除了以下内容:

  

字母,十进制数字, - _。 ! 〜*'()

如果你想使用的编码与RFC 3986兼容(其中外汇储备 * ),你可以使用:

 函数rfc3986En codeURIComponent(STR){
    返回EN codeURIComponent(STR).replace(/ ['()*!] /克,逃);
}
 

您可以得到关于MDN 这更多信息。

更新:

要回答你的问题,为什么和上面提到的其他字符都没有连接codeD由EN codeURIComponent,简单的答案是,他们只需要连接codeD在某些URI方案,并决定EN code它们取决于你所使用的方案。

要引用RFC 3986:

  

URI制作应用程序应该%的恩code数据八位字节   在保留的集合,除非这些字符对应的字符   由URI方案被特别允许重新在present数据   零件。如果保留字符是在一个URI组件,并没有发现   划定的作用是众所周知的这个角色,那么它必须是   除preTED为重presenting对应的数据字节   在US-ASCII字符的编码​​。

The escape() function, was deprecated and replaced by encodeURIComponent but encodeURIComponent doesn't encode single quote/apostrophe character. Which I need to escape the apostrophes in a persons surname (E.g. 'O'Neill') in an AJAX form. Why would they remove the ability of something they were trying to improve?

EDIT:

So here is a code example to explain the problem more thoroughly. So as you can see the surname 'O'Neill' contains an apostrophe that needs to be escaped when passing the variable in the url. But this would also happen in other places in the form, for instance if an address entered was 'Billy's Tavern'.

<input id='surname' value="O'Neill">                        
<script>
var get_url = '?surname='+encodeURIComponent($('#surname').val());
$.ajax({
    url: get_url
});
</script>

My current solution, using a custom function. My question was just to ask why there is a need for a custom function.

<script>
function customEncodeURIComponent(URI) {
    return encodeURIComponent(URI).replace(/'/g, "%27");
}
</script>

<input id='surname' value="O'Neill">
<script>
var get_url = '?surname='+customEncodeURIComponent($('#surname').val());
$.ajax({
    url: get_url
});
</script>

解决方案

encodeURIComponent escapes all characters except the following:

alphabetic, decimal digits, - _ . ! ~ * ' ( )

If you wish to use an encoding compatible with RFC 3986 (which reserves !, ', (, ), and *), you can use:

function rfc3986EncodeURIComponent (str) {  
    return encodeURIComponent(str).replace(/[!'()*]/g, escape);  
}

You can get more information on this on MDN.

UPDATE:

To answer your question, on why ' and the other chars mentioned above are not encoded by encodeURIComponent, the short answer is that they only need to be encoded in certain URI schemes and the decision to encode them depends on the scheme you're using.

To quote RFC 3986:

URI producing applications should percent-encode data octets that correspond to characters in the reserved set unless these characters are specifically allowed by the URI scheme to represent data in that component. If a reserved character is found in a URI component and no delimiting role is known for that character, then it must be interpreted as representing the data octet corresponding to that character's encoding in US-ASCII.

这篇关于为什么没有连接codeURIComponent EN code的单层引号/撇号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆