我不知道我哪里出错了。
客户端,我发送“用户”信息。
$.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'}) });