新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
nodejs使用http模塊發(fā)送get與post請(qǐng)求的方法示例
本文實(shí)例講述了nodejs使用http模塊發(fā)送get與post請(qǐng)求的方法。分享給大家供大家參考,具體如下:

10年積累的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有西烏珠穆沁免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
GET請(qǐng)求
var http = require('http');
var querystring = require('querystring');
var data = {
a: 123,
time: new Date().getTime()};//這是需要提交的數(shù)據(jù)
var content = querystring.stringify(data);
var options = {
hostname: '127.0.0.1',
port: 3000,
path: '/pay/pay_callback?' + content,
method: 'GET'
};
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
POST請(qǐng)求
var http = require('http');
var querystring = require('querystring');
var post_data = {
a: 123,
time: new Date().getTime()};//這是需要提交的數(shù)據(jù)
var content = querystring.stringify(post_data);
var options = {
hostname: '127.0.0.1',
port: 3000,
path: '/pay/pay_callback',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
};
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
//JSON.parse(chunk)
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(content);
req.end();
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
網(wǎng)站標(biāo)題:nodejs使用http模塊發(fā)送get與post請(qǐng)求的方法示例
本文路徑:http://fisionsoft.com.cn/article/jgsdig.html


咨詢
建站咨詢
