我正在使用这样的XMLHttpRequest:
xml.send(JSON.stringify({ingredients: this.state.ingredients}));
将对象(this.state.ingredients
)发送到服务器。我非常有信心它能够正确发送,因为在Chrome开发工具的“网络”标签下,请求有效负载看起来正确。但是我已经在服务器上尝试了许多不同的事情来抓住这个对象,但是我什么都没得到undefined
。
当前看起来像这样:
router.post('/recipes/:recipe_id/add', function(req, res) {
let ingredients = req.ingredients;
console.log(ingredients)
}
但我也尝试过尝试JSON.parse
其他方法。我在这里做错了什么?
如果使用express,请安装body-parser:
npm install --save body-parser
并使用它:
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
并尝试:
let ingredients = req.body.ingredients;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句