在Node.js中(使用Express.js),当我这样调用时http.request:
var options = {
host: '127.0.0.1',
port: 80,
path: '/',
query: {name: "John Doe", age: 50} // <---- problem here
};
http.request(options, function(response) { ... });
Run Code Online (Sandbox Code Playgroud)
一切都很好,除了query部分选项被忽略.文档说,查询字符串必须手动构造,并在内部传递path:类似于path: '/?name=John%20Doe&age=50'.
实现这一目标的最佳方法是什么?query是一个简单的字符串哈希 - > {string,number}.
250*_*50R 15
您正在寻找的是查询字符串库http://nodejs.org/api/querystring.html
此外,您可能对此HTTP客户端请求库https://github.com/mikeal/request感兴趣
var qs = require('querystring');
qs.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
// returns
'foo=bar&baz=qux&baz=quux&corge='
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8802 次 |
| 最近记录: |