猿问

nodejs发送http请求接受回来的数据怎么写入页面

nodejs发送http请求接受回来的数据怎么写入页面


互换的青春
浏览 507回答 3
3回答

慕运维8079593

前端代码:$(document).ready(function() {$.ajax({url: '127.0.0.1:1337', //不知道这个url到底应该填什么dataType: "jsonp",data: '{"data": "TEST"}',type: 'POST',jsonpCallback: 'callback',success: function (data) {var ret = jQuery.parseJSON(data);console.log('Success: '+ret.msg)},error: function (xhr, status, error) {console.log('Error: ' + error.message);},});});后端代码:var http = require('http');var util = require('util')http.createServer(function (req, res) {console.log('Request received: ');res.writeHead(200, { 'Content-Type': 'text/plain' });req.on('data', function (chunk) {console.log('GOT DATA!');console.log(data);});res.end('callback(\'{\"msg\": \"OK\"}\')');}).listen(1337,'127.0.0.1');console.log('Server running on port ');

小唯快跑啊

var body=reqs.body;//发送的字段{colNum,currentPage}console.log(“colNum”+JSON.stringify(body));var contentStr=queryString.stringify(body);var opt={“hostname”:url.parse(urlStr).hostname,“path”:url.parse(urlStr).path,“method”:“POST”,“port”:3000,“headers”:{.......var httpModule = urlStr.indexOf(‘https’) === 0 ? https : http;var req = httpModule.request(opt, function(httpRes) {var buffers = [];httpRes.on(‘data’, function(chunk) {buffers.push(chunk);});httpRes.on('end', function(chunk) {var wholeData = Buffer.concat(buffers);var dataStr = wholeData.toString('utf8');console.log('content'+wholeData);data=wholeData;//res.send(JSON.stringify(buffers));});}).on('error', function(err) {console.log('error ' + err);});;//写入数据,完成发送req.write(contentStr);req.end();res.send(JSON.stringify(data));
随时随地看视频慕课网APP

相关分类

Node.js
我要回答