简体   繁体   English

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

[英]How to connect my website to my node app?

So I am trying to send test data to my node app using ajax. 因此,我尝试使用ajax将测试数据发送到我的节点应用程序。 I'm not sure what I'm doing wrong to post the information. 我不确定发布信息时做错了什么。 I have added this to my script to my html: 我已经将此添加到我的html脚本中:

index.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>

I am trying to receive this information from my node app but I'm not sure how to. 我正在尝试从我的节点应用程序接收此信息,但是不确定如何做。

server.js 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');
});

However, I keep getting this error on website's console: 但是,我一直在网站的控制台上收到此错误:
'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 Error: undefined' '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(匿名函数)@ 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错误:未定义'

If this is successful I am trying to create a form that posts the input to my node app that can process it with stripe. 如果成功,那么我将尝试创建一个表单,将该表单发布到我的节点应用程序中,该应用程序可以使用Stripe处理它。

I recommend following the Express JS Getting Started guide found here: http://expressjs.com/starter/installing.html . 我建议遵循在此处找到的Express JS入门指南: http : //expressjs.com/starter/installing.html Specifically, look at the sections on Express generator and Basic routing. 具体来说,请查看有关Express生成器和基本路由的部分。

In your code, you are requiring the Express module, but not actually using it, and this module is the most robust way to handle posts in node.js. 在您的代码中,您需要Express模块​​,但实际上并未使用它,而该模块是处理node.js中帖子的最可靠的方法。

If you still want to use the http module for handling post requests, check this out: Node.js server that accepts POST requests . 如果您仍然想使用http模块来处理发布请求,请查看以下内容: 接受POST请求的Node.js服务器 It also has more information on using Express JS 它还具有有关使用Express JS的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM