为什么我无法在 Node.js 中对该 URL 进行 url 编码?

发布于 2024-12-03 09:23:19 字数 331 浏览 0 评论 0原文

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶底世界 2024-12-10 09:23:19

URI 查询中允许以明文形式使用 '。以下是相应的根据 RFC 3986 的 URI 查询的生成规则

query = *( pchar / "/" / "?" )
pchar = 未保留 / pct 编码 / 子分隔符 / ":" / "@"
未保留=字母/数字/“-”/“。” /“_”/“~”
pct 编码 = "%" HEXDIG HEXDIG
子分隔符=“!” /“$”/“&” /“'”/“(”/“)”
              /“*”/“+”/“,”/“;” /“=”

正如您所看到的,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:

query         = *( pchar / "/" / "?" )
pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded   = "%" HEXDIG HEXDIG
sub-delims    = "!" / "
quot; / "&" / "'" / "(" / ")"
              / "*" / "+" / "," / ";" / "="

As you can see, sub-delims contains a plain '. So the result is valid.

少女的英雄梦 2024-12-10 09:23:19

它编码正确,如果您手动在 google Sear 字段中输入相同的查询,您将获得此地址:

http://www.google.cz/#hl=cs&cp=8&gs_id=u&xhr=t&q=what's+up&pf=p&sclient=psy&site=&source=hp&pbx=1&oq=what's+u&aq=0&aqi=g5&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.&fp=792ecf51920895b2&biw=1276&bih=683

请注意 &q=what's+up& 部分

encodeURIComponent 是不是 Node.js 模块,而是标准 javascript 库的一部分

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

手动解决方法:

$node
querystring = require('querystring')
var dict = { 'q': 'what\'s up' };
var url = 'http://google.com/?q=' + querystring.stringify(dict);
url = encodeURIComponent(url);
url = url.replace(/'/g,"%27");

it is encoded correctly, if you type the same query into google sear field manually you will get this address:

http://www.google.cz/#hl=cs&cp=8&gs_id=u&xhr=t&q=what's+up&pf=p&sclient=psy&site=&source=hp&pbx=1&oq=what's+u&aq=0&aqi=g5&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.&fp=792ecf51920895b2&biw=1276&bih=683

note that &q=what's+up& part

and encodeURIComponent is not a Node.js module, but part of standard javascript library

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

manual workaround:

$node
querystring = require('querystring')
var dict = { 'q': 'what\'s up' };
var url = 'http://google.com/?q=' + querystring.stringify(dict);
url = encodeURIComponent(url);
url = url.replace(/'/g,"%27");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文