注意:在node / express中发布数据失败

时间:2014-07-30 03:19:02

标签: jquery node.js express

我已根据以下评论更新了问题:

我不知道我哪里出错了。

客户端,我发送“用户”信息。

$.ajax({
    type: "POST",
    url: "/user",
    data: JSON.stringify({ "user": "John"}),
    contentType : 'application/json',
    dataType: "json"
})
    .done(function() {
        alert( "done");
    })
    .fail(function() {
        alert( "fail");
    })

在服务器端,

app.use(bodyParser.json()); 

app.post('/user', function (req, res) {
    console.log(req.body.user);
    res.end();
});

这会导致console.log正确录制“John”,但在客户端,我仍然会收到“失败”警报。无法弄清楚原因。

我正在使用node,express和jquery的最新版本。

感谢。

更新

缺少这个:

res.contentType('json');
res.send({ some: JSON.stringify({response:'json'}) });

0 个答案:

没有答案