CRUD nodejs/express服务器:app.put req.body为空 [英] CRUD nodejs/express server: app.put req.body is empty
本文介绍了CRUD nodejs/express服务器:app.put req.body为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚用基本的Crud操作编写了一个超简单的nodejs后端.但是,在put语句上,我没有收到请求的正文:
I just wrote a ultrasimple nodejs backend with basic crud operations. However on the put statement i don't receive the body of my request:
app.put('/note-update/:id', function (req, res) {
var noteId = req.params.id;
console.log(req.body)
db.collection('notes').update({
_id: noteId
}, req.body, (err, result) => {
res.send(
(err === null) ? {
msg: req.body
} : {
msg: err
}
);
});
});
这是我使用ajax执行的呼叫:
Here is the call i execute with ajax:
var note = {
_id: "599e660cbc845b4e2952715f",
name: "Genauer Detailbeschrieb",
note: "Ey-10"
}
$.ajax({
type: 'PUT',
url: '/note-update/599e660cbc845b4e2952715f',
dataType: 'JSON',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(note)
})
如您所见,我有一个console.log(req.body),我得到的只是{}
as you can see i have a console.log(req.body) and all i get is {}
推荐答案
您在使用bodyparser吗?
Are you using the bodyparser?
app.use(bodyParser.json());
如果不起作用,请为您的节点设置显示更多代码.
Plz show more code for your node setup if it doesn't work.
这篇关于CRUD nodejs/express服务器:app.put req.body为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文