如何将我的网站连接到我的节点应用程序?

所以我试图发送testing数据到我的节点应用程序使用Ajax。 我不知道我在做什么错误发布的信息。 我已经添加到我的脚本到我的HTML:

的index.html

<script type="text/javascript"> jQuery(function() { console.log('hello'); var $ = jQuery; $(window).ready(function() { console.log('hello'); $.ajax({ dataType: 'jsonp', xhrFields: { withCredentials: true, }, url: 'http://localhost:3000', data: '{"data": "TEST"}', type: 'POST', success: function () { console.log('Success: '); }, error: function (xhr, status, error) { console.log('Error: ' + error.message); }, }); }); }); </script> 

我试图从我的节点应用程序接收这个信息,但我不知道如何。

server.js

 var express = require('express') , cors = require('cors') , app = express() , http = require('http'); app.use(cors()); var server = http.createServer(app, function(req, res) { var body = ""; req.on('data', function (chunk) { body += chunk; }); req.on('end', function () { console.log(body); res(body); }); }).listen(3000, function(){ console.log('CORS-enabled web server listening on port 80'); }); 

但是,我不断收到这个错误在网站的控制台上:
'GET http:// localhost:3000 /?callback = jQuery214011563337640836835_1442781076103& {%22data%22:%20%22TEST%22}&_ = 1442781076104 n.ajaxTransport.a.send @ jquery.js:8698n.extend.ajax @ jquery。 js:8166(anonymous function)@ final.html:534n.Callbacks.j @ jquery.js:3099n.Callbacks.k.fireWith @ jquery.js:3211n.extend.ready @ jquery.js:3417I @ jquery.js: 3433 final.html:550错误:undefined'

如果这是成功的,我正在尝试创build一个窗体,将input张贴到我的节点应用程序,可以使用条纹处理它。

我build议遵循下面的Express JS入门指南: http : //expressjs.com/starter/installing.html 。 具体来说,请查看Express生成器和基本路由的章节。

在你的代码中,你需要Express模块​​,但是实际上并没有使用它,而且这个模块是处理node.js中最可靠的方法。

如果您仍然想使用http模块来处理post请求,请检查: 接受POST请求的Node.js服务器 。 它也有使用Express JS的更多信息