gre*_*emo 0 javascript http httprequest node.js
开始学习Node.js,POST用Node.js 发送请求:
var http = require('http')
, https = require('https')
, _ = require('underscore')
, querystring = require('querystring');
// Client constructor ...
Client.prototype.request = function (options) {
_.extend(options, {
hostname: Client.API_ENDPOINT,
path: Client.API_PATH,
headers: {
'user-agent': this.agent
}
});
var req = (this.secure ? https : http).request(options);
if(options.data) req.write(querystring.stringify(options.data));
req.end();
req.on('response', function (res) {
res.on('data', function (chunk) {
res.body += chunk;
});
res.on('end', function () {
console.log(res.body);
});
});
}
Run Code Online (Sandbox Code Playgroud)
身体表演:undefined<xml version="1.0" encoding="UTF-8">.
哪里undefined来的?
fre*_*ish 12
您必须res.body在添加之前进行初始化:
// some other code
req.on('response', function (res) {
res.body = "";
res.on('data', function (chunk) {
res.body += chunk;
});
res.on('end', function () {
console.log(res.body);
});
});
Run Code Online (Sandbox Code Playgroud)
否则,您要添加到undefined哪个转换undefined为字符串"undefined".
| 归档时间: |
|
| 查看次数: |
5972 次 |
| 最近记录: |