【问题标题】:php to node.js ajax sending does not give responsephp到node.js ajax发送不响应
【发布时间】:2015-08-20 15:24:11
【问题描述】:

我试图将一些数据从我的 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 页面重定向它

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

有人可以在 console.log 中帮助我吗?我收到 {} .....

【问题讨论】:

  • php 代码行中变量“value”的值是什么 ::>>> var value = document.getElementById("code").value;
  • ok 你知道node js中的调试吗
  • @Hiren “代码”是我将从 HTML 页面获得的值
  • @Ahmer 不合格兄弟
  • 假设您在文本框中输入了一些文本,如果不是 JSON 值,则不会在 node.js 中解析。所以你想在你的控制器中得到结果

标签: javascript php jquery ajax node.js


【解决方案1】:

第一次测试是不是前端有问题。

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);
                }
            });   
}

你应该在你的 express 应用中设置 json body parser

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

你在哪里声明了data 变量。在 node.js 中,通过 ajax 请求发送的数据在 req.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);
    },
});

【讨论】:

  • 先生,我已经做到了..但这会让我{}胖加雅亚尔
  • 是的,先生,我已经通过 contentType 修复了它:“application/x-www-form-urlencoded”,//这就是与众不同的原因。行
  • 您将数据作为字符串发送,这可能是问题所在。一旦检查更新的答案
猜你喜欢
  • 2012-04-24
  • 1970-01-01
  • 2020-07-25
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 2015-05-02
  • 2018-11-02
相关资源
最近更新 更多