您的位置:首页 > 理论基础 > 计算机网络

node.js以post请求方式发送http请求

2015-09-15 16:40 691 查看
var http = require('http');
var querystring = require('querystring');
// 请求参数
var postData = querystring.stringify({
'name': "wangbin",
'password': "123456",
'serverId': 2
});
var options = {
hostname: '192.168.1.135',
port: 3001,
path: '/login',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var resData = [];
res.on('data', function (chunk) {
resData.push(chunk);
});
res.on('end', function() {
var data = resData.join("");
console.log(data);
})
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// 发送请求
req.write(postData);
req.end();
本文出自 “mirwangsir” 博客,请务必保留此出处http://mirwangsir.blog.51cto.com/7723278/1695008
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: