为什么不使用encodeURIComponent编码单引号/撇号? [英] Why doesn't encodeURIComponent encode single quotes/apostrophes?

查看:855
本文介绍了为什么不使用encodeURIComponent编码单引号/撇号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

escape()函数已被弃用,并被 encodeURIComponent 取代,但 encodeURIComponent 不会对单引号/撇号字符进行编码。我需要以AJAX形式逃避姓氏(例如'O'Neill')中的撇号。为什么他们会删除他们试图改进的东西?

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?

编辑:

所以这是一个代码示例更彻底地解释问题。因此,您可以看到姓氏'O'Neill'包含一个撇号,需要在传递url中的变量时进行转义。但这也会发生在表格中的其他地方,例如,如果输入的地址是'Billy's Tavern'。

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 转义除以下内容之外的所有字符:

encodeURIComponent escapes all characters except the following:


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

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

如果您希望使用与RFC 3986兼容的编码(保留' * ),您可以使用:

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 :

要回答您的问题,为什么'以及上面提到的其他字符未编码通过encodeURIComponent,简短的回答是它们只需要在某些URI方案中编码,并且编码它们的决定取决于你正在使用的方案。

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.

引用 RFC 3986


URI生成应用程序应对
对应于保留集中字符的数据八位字节进行百分比编码,除非特别允许这些字符
通过URI方案表示该
组件中的数据。如果在URI组件中找到保留字符且该字符没有
分隔角色,则必须将
解释为表示与US
字符的编码对应的数据八位字节。 ASCII。

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.

其中保留集定义为

reserved    = gen-delims / sub-delims
gen-delims  = ":" / "/" / "?" / "#" / "[" / "]" / "@"
sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
            / "*" / "+" / "," / ";" / "="

Apostrophe位于子delims 组。换句话说,如果您确定消费应用程序将知道如何处理它们,则必须将这些字符保留为未编码,例如,如果您错误地编码& 他们将不再界定查询部分。从历史上看,还有建议用; 分隔的路径段参数(没有大量采用),所以这些角色也是允许的。在URI数据中并不是指令可以自由使用(即未保留),但是假设它在URI上下文中具有一些特殊含义,例如部分:

Apostrophe is in the sub-delims group. In other words, you must leave these characters unencoded expecially if you are sure that consuming applications will know what to do with them: for example if you mistakenly encoded ? and & they will no longer delimit query parts. Historically there were also proposal for path segments parameters delimited with ; and , (didn't get large adoption), so these characters are also still allowed,. It is not that apostrohe is "free to use" (ie unreserved) in URI data, but that it was assumed it will have some special meaning in the URI context, for example the segment part:

segment       = *pchar
pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"

这篇关于为什么不使用encodeURIComponent编码单引号/撇号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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