javascript - 为什么我不能在 node.js 中对这个 URL 进行 url 编码?

标签 javascript url encoding 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);

结果是这样的:

"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/

相关文章:

java - 对 URL 中的数据进行编码以实现高效传输

javascript - PHP 在 JavaScript 中使用 CryptoJS openssl_encrypt

c++ - Windows 中的 C++ 是否内置了对 UTF 的支持?

javascript - 是否可以在 chrome 调试器中操作返回值?

c++ - 文件下载器建议

Javascript 计时器,带停止按钮

java用代理打开url需要很长时间

python - python向mysql插入中文字符时出现UnicodeEncodeError

javascript - Lerna bootstrap 不链接本地依赖项?

javascript - 对象的属性在 Array.map 中不可用(未定义)