如何用NodeJS的http模块获取ajax返回的json数据

如何用NodeJS的http模块获取ajax返回的json数据


长风秋雁
浏览 1891回答 3
3回答

慕妹3242003

require('http').createServer(function (req, res1) {//res1.writeHead(200, {'Content-Type': 'text/plain'});var request = require('./request');request.request('login',{account: require('./params').account,password: require('./params').password}, function(res){res1.writeHead(200, {'Content-Type': 'text/plain'});res1.write( res.body );res1.end();});}).listen(8080, "127.0.0.1"); 

烙印99

前端代码:$(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 ');  
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Node.js