I send request, but in req. The body is empty {}, although I see that the data is leaving normally. The part that does not work from the user is recorded, but is set by me by default. Where did I miss something?
Request body:
document.forms.ourform.onsubmit = function(e){ e.preventDefault(); var userInput = document.forms.ourform.textarea.value; var xhr = new XMLHttpRequest(); xhr.open('POST', 'http://localhost:3000/coffee'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;'); xhr.send(userInput); } Handler:
app.post('/coffee', coffeController.create); exports.create = function(sort, callback){ db.get().collection('coffee').insert(sort, function(err, result) { callback(err, result); }) } exports.create = function(req, res){ var sort = { *//Вот здесь я получаю пустой {}, вместо body* sort: req.body, costs: { amount3: 50, amount5: 70 } } coffee.create(sort, function(err, result){ if (err) { console.log(err); return res.sendStatus(500); } res.send(sort); }) } I apologize for the hodgepodge of code.
x-www-form-urlencodedformat should bename=value, and you only sendvalue. - Alexey Ten