TIM*_*MEX 294 javascript url node.js
我想对URL进行编码:
SELECT name FROM user WHERE uid = me()
Run Code Online (Sandbox Code Playgroud)
我必须为此下载模块吗?我已经有了请求模块.
Joe*_*Joe 557
你可以使用JavaScript encodeURIComponent
:
encodeURIComponent('select * from table where i()')
Run Code Online (Sandbox Code Playgroud)
nic*_*ten 124
querystring
您正在寻找内置模块:
var querystring = require("querystring");
var result = querystring.stringify({query: "SELECT name FROM user WHERE uid = me()"});
console.log(result);
#prints 'query=SELECT%20name%20FROM%20user%20WHERE%20uid%20%3D%20me()'
Run Code Online (Sandbox Code Playgroud)
Kam*_*rul 46
使用的escape
功能querystring
.它生成一个URL安全字符串.
var escaped_str = require('querystring').escape('Photo on 30-11-12 at 8.09 AM #2.jpg');
console.log(escaped_str);
// prints 'Photo%20on%2030-11-12%20at%208.09%20AM%20%232.jpg'
Run Code Online (Sandbox Code Playgroud)
Fli*_*imm 17
请注意,URI编码适用于查询部分,但它对域不利.域使用punycode进行编码.您需要像URI.js这样的库来在URI和IRI(国际化资源标识符)之间进行转换.
如果您计划稍后将该字符串用作查询字符串,这是正确的:
> encodeURIComponent("http://examplé.org/rosé?rosé=rosé")
'http%3A%2F%2Fexampl%C3%A9.org%2Fros%C3%A9%3Fros%C3%A9%3Dros%C3%A9'
Run Code Online (Sandbox Code Playgroud)
如果你不想ASCII字符像/
,:
并?
进行转义,使用encodeURI
来代替:
> encodeURI("http://examplé.org/rosé?rosé=rosé")
'http://exampl%C3%A9.org/ros%C3%A9?ros%C3%A9=ros%C3%A9'
Run Code Online (Sandbox Code Playgroud)
但是,对于其他用例,您可能需要使用uri-js:
> var URI = require("uri-js");
undefined
> URI.serialize(URI.parse("http://examplé.org/rosé?rosé=rosé"))
'http://xn--exampl-gva.org/ros%C3%A9?ros%C3%A9=ros%C3%A9'
Run Code Online (Sandbox Code Playgroud)
Joh*_*ner 11
encodeURIComponent(string)会这样做:
encodeURIComponent("Robert'); DROP TABLE Students;--")
//>> "Robert')%3B%20DROP%20TABLE%20Students%3B--"
Run Code Online (Sandbox Code Playgroud)
在查询字符串中传递SQL可能不是一个好的计划,
归档时间: |
|
查看次数: |
259228 次 |
最近记录: |