ajax 为什么 encodeURIComponent 不编码单引号/撇号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18251399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't encodeURIComponent encode single quotes/apostrophes?
提问by Con Tully
The escape()function, was deprecated and replaced by encodeURIComponentbut encodeURIComponentdoesn'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?
该逃生()函数,已被废弃,取而代之encodeURIComponent方法,但encodeURIComponent方法不编码单引号/单引号字符。我需要以 AJAX 形式转义人名(例如“O'Neill”)中的撇号。他们为什么要删除他们试图改进的东西的能力?
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'.
所以这里有一个代码示例来更彻底地解释这个问题。因此,如您所见,姓氏“O'Neill”包含一个撇号,在 url 中传递变量时需要对其进行转义。但这也会发生在表单的其他地方,例如,如果输入的地址是“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>
回答by dcro
encodeURIComponentescapes all characters except the following:
encodeURIComponent转义除以下字符外的所有字符:
alphabetic, decimal digits, - _ . ! ~ * ' ( )
字母、十进制数字, - _ 。!~ * ' ( )
If you wish to use an encoding compatible with RFC 3986 (which reserves !, ', (, ), and *), you can use:
如果您希望使用兼容的编码与RFC 3986(其中储备!,',(,),和*),您可以使用:
function rfc3986EncodeURIComponent (str) {
return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}
You can get more information on this on MDN.
您可以在 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.
要回答您的问题,为什么'上面提到的字符和其他字符不是由 encodeURIComponent 编码的,简短的回答是它们只需要在某些 URI 方案中进行编码,对它们进行编码的决定取决于您使用的方案。
To quote RFC 3986:
引用RFC 3986:
URI producing applications should percent-encode data octets that correspond to characters in the
reserved setunless 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.
URI 生成应用程序应该对与
reserved set. 如果在 URI 组件中找到保留字符,并且该字符没有已知的定界角色,则必须将其解释为表示与该字符在 US-ASCII 中的编码相对应的数据八位字节。
Where "reserved set" is defined as
其中“保留集”定义为
reserved = gen-delims / sub-delims
gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
Apostrophe is in the sub-delimsgroup. 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 segmentpart:
撇号在sub-delims组中。换句话说,如果您确定使用这些字符的应用程序知道如何处理它们,则必须特别保留这些字符未编码:例如,如果您错误地编码?并且&它们将不再分隔查询部分。历史上也有过用;and分隔的路径段参数的提议,(没有得到大量采用),所以这些字符也仍然允许,。并不是说撇号unreserved在 URI 数据中是“可自由使用的”(即),而是假设它在 URI 上下文中具有某些特殊含义,例如以下segment部分:
segment = *pchar
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
回答by Amith
try this
尝试这个
encodeURIComponent(str).replace(/'/g, "%27");
The /char/gsyntax tells JavaScript to replace all occurrences in your string
该/char/g语法告诉JavaScript来取代所有出现在你的字符串
回答by Sylvie Shamir
I saw there is the same problem with the chars: "and \so that's what worked for me:
我看到有一个与字符同样的问题:"和\使对我工作:
var replaceChars={ '\':'\\' , '"':'\"' };
encodeURIComponent(str.replace(/\|"/gi, function(matched){
return replaceChars[matched];
})),
回答by Александр
this helps me:
这对我有帮助:
replace(/'/g, '%60')
%60 is ` , but somehow defined as a single quote.
%60 是 ` ,但以某种方式定义为单引号。

