[英]NodeJS Express Req.Body is undefined
I have worked with several solutions posted on stack overflow, and I have yet to find one to work with me so I decided to ask. 我已经处理了堆叠溢出的几个解决方案,我还没有找到一个与我一起工作,所以我决定问。
Here is my nodejs file: (leaving out irrelevant code) 这是我的nodejs文件:(省略不相关的代码)
var express = require('express'); var app = express(); var cors = require('cors'); var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded()); app.use(bodyParser.json()); app.use(cors()); app.get('/sendEmail', function(req, res) { var answers = req.body.answers; console.log(answers); })
and also my POST: 还有我的帖子:
$http.get('http://localhost:5000/sendEmail', { answers: answer })
How come when I console log req.body.answers
I get undefined? 为什么我控制日志
req.body.answers
我得到未定义?
You cannot have a body in a GET
request. 您不能在
GET
请求中拥有正文。 So change this: 所以改变这个:
app.post('/sendEmail', function(req, res) {
var answers = req.body.answers;
// whatever
})
and this: 和这个:
$http.post('http://localhost:5000/sendEmail', {
answers: answer
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.