node.js使用HTTP发送post请求方法
var http = require('http');
function post(action,send,callback){
var options = {
hostname: '127.0.0.1',
port: 80,
path: action,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
};
var req = http.request(options, function (res) {
var body="";
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function(){
var json = JSON.parse(body);
callback(json);
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.write(send);
req.end();
}
post('/node.php',"id="+id,function(json){
console.log(json);
});