nodejs post方式获取数据
nodejshttp模块儿关于get数据的形式可以很简单实现。但涉及到post方式的怎么做呢,网上的资料都是说下面这种方式,但我试过不行无法接收到post参数,有人确定是...
nodejs http模块儿关于get数据的形式可以很简单实现。但涉及到post方式的怎么做呢,网上的资料都是说下面这种方式,但我试过不行无法接收到post参数,有人确定是这样吗?希望有人能帮助解答下。下面是经过测试不生效的代码:
var data = {
name:'zhangsan' ,
age:30
};
data = require('querystring').stringify(data);
console.log(data);
var opt = {
method: "POST",
host: "localhost",
port: 8080,
path: "/v1/sendEmail",
headers: {
"Content-Type": 'application/x-www-form-urlencoded',
"Content-Length": data.length
}
};
var http = require(http);
var req = http.request(opt, function (serverFeedback) {
if (serverFeedback.statusCode == 200) {
var body = "";
serverFeedback.on('data', function (data) { body += data; })
.on('end', function () {
console.log('over=' + body);
});
}
});
req.write(data + "\n");
req.end();
有人确认req.write();是可行的吗?
对了还有 host 只可以是ip吗,我用域名就会报错
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16) 展开
var data = {
name:'zhangsan' ,
age:30
};
data = require('querystring').stringify(data);
console.log(data);
var opt = {
method: "POST",
host: "localhost",
port: 8080,
path: "/v1/sendEmail",
headers: {
"Content-Type": 'application/x-www-form-urlencoded',
"Content-Length": data.length
}
};
var http = require(http);
var req = http.request(opt, function (serverFeedback) {
if (serverFeedback.statusCode == 200) {
var body = "";
serverFeedback.on('data', function (data) { body += data; })
.on('end', function () {
console.log('over=' + body);
});
}
});
req.write(data + "\n");
req.end();
有人确认req.write();是可行的吗?
对了还有 host 只可以是ip吗,我用域名就会报错
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16) 展开
1个回答
展开全部
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询