为什么我无法在 Node.js 中对该 URL 进行 url 编码?
$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 模块有问题吗?
$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);
The result is this:
"http://google.com/?q=q=what's%20up"
Notice how the single quote is not encoded correctly. Is there something wrong with the node.js module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
URI 查询中允许以明文形式使用
'
。以下是相应的根据 RFC 3986 的 URI 查询的生成规则 :正如您所看到的,sub-delims 包含一个普通的
'
。所以结果是有效的。The
'
is allowed in plain in the URI query. Here are the corresponding production rules for the URI query as per RFC 3986:As you can see, sub-delims contains a plain
'
. So the result is valid.它编码正确,如果您手动在 google Sear 字段中输入相同的查询,您将获得此地址:
请注意
&q=what's+up&
部分和
encodeURIComponent
是不是 Node.js 模块,而是标准 javascript 库的一部分https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
手动解决方法:
it is encoded correctly, if you type the same query into google sear field manually you will get this address:
note that
&q=what's+up&
partand
encodeURIComponent
is not a Node.js module, but part of standard javascript libraryhttps://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
manual workaround: