繁体   English   中英

Express.js req.body 未定义

[英]Express.js req.body is undefined

编辑:伙计们,我对这一切都很陌生。 这是我使用的 html 表格。 我应该用其他东西更新这个问题吗?

<form action="/pesquisar" method="post">
    <input type="text" id="cO">
    <input type="text" id="cD">
    <input type="submit">
</form>

我目前正在尝试使用 express 设计一个简单的浏览器应用程序。 console.log(req.body) 回来{},我找不到解决方案,在这里度过了一天中最美好的时光哈哈

这是我的应用程序

var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');

var app = express();

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');



app.get('/', function(req, res){
    console.log(req.body);

    res.render('index', {
        infoVoos: 'acesso_inicial'
    });
});

app.post('/pesquisar', function(req,res){

    console.log("");
    console.log("");
    console.log(req.body);

   
            res.send('ok');    
});

app.listen(3000);

console.log('############');
console.log('Server on');
console.log('');

module.exports = app;

我将输入标签的参数从“id”更改为“name”,现在它工作正常。

原来的

<form action="/pesquisar" method="post">
    <input type="text" id="cO">
    <input type="text" id="cD">
    <input type="submit">
</form>

新的

<form action="/pesquisar" method="post">
    <input type="text" name="cO">
    <input type="text" name="cD">
    <input type="submit">
</form>

谢谢各位

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM