$node
querystring = require('querystring')
var dict = { 'q': 'what\'s up' };
var url = 'http://google.com/?q=' + querystring.stringify(dict);
url = encodeURIComponent(url);
console.log(url);
结果是这样的:
"http://google.com/?q=q=what's%20up"
注意单引号是如何编码不正确的。 node.js 模块有问题吗?
最佳答案
'
在 URI 查询中是允许的。下面是对应的production rules for the URI query as per RFC 3986 :
query = *( pchar / "/" / "?" ) pchar = unreserved / pct-encoded / sub-delims / ":" / "@" unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" pct-encoded = "%" HEXDIG HEXDIG sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
如您所见,sub-delims 包含一个普通的'
。所以结果是有效的。
关于javascript - 为什么我不能在 node.js 中对这个 URL 进行 url 编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7299149/