我如何在 node.js 中将 URL 作为查询字符串参数。
例如:当我使用以下网址点击浏览器时:
http://localhost:3000/key1/https://www.google.com/key2/c,我无法将 value1 获取为“ https://www.google.com ”。我在浏览器中遇到错误,说无法获取查询字符串。但是如果我用一些随机字符串或数字替换 url ,我的代码就可以工作。
它不起作用的原因是你的 url 参数不是 url 编码的。如果您有斜杠或各种其他符号,例如 :,它会破坏您的参数。
例如,
param1 = thisisastring,
param2 = thisisalsoastring
<ip:port>/param1/param2
好的!
param1 = thisisastring/somethingelse,
param2 = thisisalsoastring
<ip:port>/param1/param2
破碎的!
为什么?
快递看到3个参数:
thisisastring somethingelse thisisalsoastring
解决方案:URL 编码您的请求。有很多图书馆可以做到这一点。
https://www.google.com应编码为:https%3A%2F%2Fwww.google.com
注意没有斜线或冒号。;)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句