通过http.request发送带参数的post请求
data:发送的内容
opt:描述将要发出的请求
data:事件在数据到达时被触发
end:请求结束时触发
error:发生错误时被触发
var http = require("http");
var data = {username:"hello",password:"123456"};
data = JSON.stringify(data);
//data = require('querystring').stringify(data);
var opt = {
host:'localhost',
port:'8080',
method:'POST',
path:'/loginForeign.jspx',
headers:{
"Content-Type": 'application/json',
"Content-Length": data.length
}
}
var body = '';
var req = http.request(opt, function(res) {
console.log("response: " + res.statusCode);
res.on('data',function(data){
body += data;
}).on('end', function(){
console.log(body)
});
}).on('error', function(e) {
console.log("error: " + e.message);
})
req.write(data);
req.end();