[英]I've created a node app through cloud 9, but how do I connect my app with my website?
[英]How to connect my website to my node app?
因此,我嘗試使用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(匿名函數)@ 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錯誤:未定義'
如果成功,那么我將嘗試創建一個表單,將該表單發布到我的節點應用程序中,該應用程序可以使用Stripe處理它。
我建議遵循在此處找到的Express JS入門指南: http : //expressjs.com/starter/installing.html 。 具體來說,請查看有關Express生成器和基本路由的部分。
在您的代碼中,您需要Express模塊,但實際上並未使用它,而該模塊是處理node.js中帖子的最可靠的方法。
如果您仍然想使用http模塊來處理發布請求,請查看以下內容: 接受POST請求的Node.js服務器 。 它還具有有關使用Express JS的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.