php到node.js ajax发送不给响应

我试图从我的PHP页面发布一些数据到我的node.js服务器,我想从中得到响应。

这是我从我当前在apache服务器上执行的php页面发送的ajax代码

function encryptCode() { var value = document.getElementById("code").value; $.ajax({ url: "http://localhost:3000/insertUser", type: "POST", data: JSON.stringify(value), dataType: 'json', async: false, contentType: 'application/json; charset=utf-8', success: function(data) { alert(data); } }); } 

而我只是想通过此代码在我的node.js页面中接收它

 var BaseController = require("./Base"), View = require("../views/Base"), model = new (require("../models/ContentModel")); module.exports = BaseController.extend({ name: "insertUser", content: null, run: function(req, res, next) { model.setDB(req.db); var self = this; console.log(data); /*this.getContent(function() { // var v = new View(res, 'place'); res.setHeader('Content-Type', 'application/json'); res.send(JSON.stringify(self.content)); });*/ // console.log("go to hell"); }, }); 

这是我的express.js的控制器,我用我的app.js页面redirect了这个代码

 app.all('/insertUser', attachDB, function(req, res, next) { insertUser.run( req, res, next); }); 

有人请帮助我在console.log我越来越{…}

第一个testing是前端的问题。

 function encryptCode() { var value = document.getElementById("code").value; console.log(value); $.ajax({ url: "http://localhost:3000/insertUser", type: "POST", data: {"user":value}, dataType: 'json', async: false, contentType: 'application/json; charset=utf-8', success: function(data) { alert(data); } }); } 

你应该在你的快速应用程序中设置json body parser

 var app = express(); bodyParser = require('body-parser'); app.use(bodyParser.json()); 

你在哪里声明datavariables。 在node.js中,通过ajax请求发送的数据在req.bodyreq.body

 var BaseController = require("./Base"), View = require("../views/Base"), model = new (require("../models/ContentModel")); module.exports = BaseController.extend({ name: "insertUser", content: null, run: function(req, res, next) { model.setDB(req.db); var self = this; console.log(req.body); // console.log(req.body.user); }, });