【问题标题】:ExpressJS jQuery ajax Post returns nothingExpressJS jQuery ajax Post 什么也不返回
【发布时间】:2016-01-21 05:49:06
【问题描述】:

我正在尝试从 Ajax 发布请求中返回一些值,但我在控制台中看不到任何值。这里缺少什么?

快递

app.post('/register',function(req,res) {
    //console.log(JSON.stringify(req.body)); // it works
    res.send(req.body); // it doesn't work on client side.
});

jQuery

$("#register").submit(function (e) {
    e.preventDefault();
    var data = {};
    data.username = $('#username').val();
    data.email = $('#email').val();
    data.password1 = $('#password1').val();
    data.password2 = $('#password2').val();
    if (data.password1 == data.password2) {
        alert("okay");
        $.ajax({
            type: 'POST',
            data: JSON.stringify(data),
            contentType: 'application/json',
            url: '/register',
            success: function (data) {
                console.log(data.username + data.email + data.password1 + data.password2); 
                // nothing returns, why?
            }
        });
    } else {
        $('.register-now').after('<div class="alert alert-danger" role="alert"> ' +
            'You entered two different passwords. ' +
            '</div>');
    }
});

【问题讨论】:

    标签: jquery json node.js express


    【解决方案1】:

    我很困惑,你说

    但我在控制台中看不到任何值

    然后你说

    console.log(JSON.stringify(req.body)); // 有效

    什么给了?

    【讨论】:

    • 我明白了。这是个好问题,我要睡觉了……在 Chrome 中,网络开发工具中发生了什么?
    【解决方案2】:

    我查看的是终端而不是浏览器控制台。它没有任何问题;它有效。

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 2013-07-24
      • 2015-11-18
      • 2017-12-11
      • 2011-04-07
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多