1
我剛剛寫了一個超簡單nodejs後端與基本crud操作。然而在PUT語句,我不接受我的請求主體:CRUD nodejs /快遞服務器:app.put req.body爲空
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執行調用:
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),我得到的全部是{}
您是否使用'bodyParser'中間件來表達? –
是你的**職位** Http方法的工作?如果不使用** BodyParser **中間件 –
是啊,後工作與bodyParser,雖然我從來沒有嘗試與ajax –