如何将数据从JQuery AJAX请求发送到Node.js服务器

我想做的事:
只需发送一些数据(例如json)到一个node.js http服务器,使用jquery ajax请求。

出于某种原因,我不能设法获取服务器上的数据,因为它永远不会触发请求的“数据”事件。

客户代码:

$.ajax({ url: server, dataType: "jsonp", data: '{"data": "TEST"}', jsonpCallback: 'callback', success: function (data) { var ret = jQuery.parseJSON(data); $('#lblResponse').html(ret.msg); }, error: function (xhr, status, error) { console.log('Error: ' + error.message); $('#lblResponse').html('Error connecting to the server.'); } }); 

服务器代码:

 var http = require('http'); 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!'); }); res.end('callback(\'{\"msg\": \"OK\"}\')'); }).listen(8080, '192.168.0.143'); console.log('Server running at http://192.168.0.143:8080/'); 

正如我所说,它永远不会进入请求的“数据”事件。

注释:
1.logging“请求已收到”消息;
2.回复很好,能够在客户端处理数据;

任何帮助? 我错过了什么?

谢谢大家。

编辑:
根据回答评论代码的最终版本:

客户代码:

 $.ajax({ type: 'POST' // added, url: server, data: '{"data": "TEST"}', //dataType: 'jsonp' - removed //jsonpCallback: 'callback' - removed success: function (data) { var ret = jQuery.parseJSON(data); $('#lblResponse').html(ret.msg); }, error: function (xhr, status, error) { console.log('Error: ' + error.message); $('#lblResponse').html('Error connecting to the server.'); } }); 

服务器代码:

 var http = require('http'); http.createServer(function (req, res) { console.log('Request received'); res.writeHead(200, { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*' // implementation of CORS }); req.on('data', function (chunk) { console.log('GOT DATA!'); }); res.end('{"msg": "OK"}'); // removed the 'callback' stuff }).listen(8080, '192.168.0.143'); console.log('Server running at http://192.168.0.143:8080/'); 

由于我想允许跨域请求,我添加了一个CORS的实现。

谢谢!

为了让'data'事件在node.js服务器端触发,你必须POST数据。 也就是说,'data'事件只响应POST数据。 指定'jsonp'作为数据格式强制GET请求,因为jsonp在jquery文档中定义为:

“jsonp”:使用JSONP加载JSON块。 添加额外的“?callback =?” 到您的URL的末尾来指定callback

以下是您如何修改客户端以启动您的数据事件。

客户:

 <html> <head> <script language="javascript" type="text/javascript" src="jquery-1.8.3.min.js"></script> </head> <body> response here: <p id="lblResponse">fill me in</p> <script type="text/javascript"> $(document).ready(function() { $.ajax({ url: 'http://192.168.0.143:8080', // dataType: "jsonp", data: '{"data": "TEST"}', type: 'POST', jsonpCallback: 'callback', // this is not relevant to the POST anymore success: function (data) { var ret = jQuery.parseJSON(data); $('#lblResponse').html(ret.msg); console.log('Success: ') }, error: function (xhr, status, error) { console.log('Error: ' + error.message); $('#lblResponse').html('Error connecting to the server.'); }, }); }); </script> </body> </html> 

一些有用的线路可以帮助您debugging服务器端:

服务器:

 var http = require('http'); var util = require('util') http.createServer(function (req, res) { console.log('Request received: '); util.log(util.inspect(req)) // this line helps you inspect the request so you can see whether the data is in the url (GET) or the req body (POST) util.log('Request recieved: \nmethod: ' + req.method + '\nurl: ' + req.url) // this line logs just the method and url res.writeHead(200, { 'Content-Type': 'text/plain' }); req.on('data', function (chunk) { console.log('GOT DATA!'); }); res.end('callback(\'{\"msg\": \"OK\"}\')'); }).listen(8080); console.log('Server running on port 8080'); 

在节点端的数据事件的目的是build立正文 – 每一个http请求触发多次,为它接收到的每个数据块一次。 这是node.js的asynchronous性质 – 服务器在接收数据块之间做其他工作。