3 回答
TA贡献1876条经验 获得超5个赞
前端代码:
$(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 ');
TA贡献1863条经验 获得超2个赞
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));
- 3 回答
- 0 关注
- 502 浏览
添加回答
举报