新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
nodejs使用http模塊發(fā)送get與post請求的方法示例
本文實例講述了nodejs使用http模塊發(fā)送get與post請求的方法。分享給大家供大家參考,具體如下:
10年積累的成都網(wǎng)站設計、成都網(wǎng)站制作經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先網(wǎng)站設計后付款的網(wǎng)站建設流程,更有西烏珠穆沁免費網(wǎng)站建設讓你可以放心的選擇與我們合作。
GET請求
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請求
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();
希望本文所述對大家nodejs程序設計有所幫助。
網(wǎng)站欄目:nodejs使用http模塊發(fā)送get與post請求的方法示例
URL地址:http://fisionsoft.com.cn/article/jgsdig.html