本文介绍了为什么不使用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:

如果您希望使用与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);
}

您可以获得更多相关信息:

其中保留集定义为

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编码单引号/撇号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 19:29