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'/* ,
'Content-Length': send.length */
}
};
var req = http.request(options, function (res) {
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
// 定义了一个post变量,用于暂存请求体的信息
var body="";
res.setEncoding('utf8');
// 通过res的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
res.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
body += chunk;
});
// 在res的end事件触发后,通过JSON.parse将post解析为真正的POST请求格式,然后调用传递过来的回调函数处理数据
res.on('end', function(){
// console.log("body = "+body);
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函数在nodejs里发送post请求
post('/node.php',"id="+id,function(json){
console.log(json);
});