【问题标题】:NodeJS Express Req.Body is undefinedNodeJS Express Req.Body 未定义
【发布时间】:2017-11-16 00:18:05
【问题描述】:

我已经使用过几种关于堆栈溢出的解决方案,但我还没有找到一个可以与我一起使用的解决方案,所以我决定问一下。

这是我的 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);
})

还有我的帖子:

$http.get('http://localhost:5000/sendEmail', {
  answers: answer
})

为什么当我控制台日志req.body.answers 我得到未定义?

【问题讨论】:

    标签: angularjs node.js ionic-framework undefined


    【解决方案1】:

    GET 请求中不能有正文。所以改变这个:

    app.post('/sendEmail', function(req, res) {
        var answers = req.body.answers;
        // whatever
    })
    

    还有这个:

    $http.post('http://localhost:5000/sendEmail', {
      answers: answer
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 2018-11-07
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多