Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

javascript - Express.js req.body is undefined

edit:guys I'm genually new to all of this. here's the html form I used. should i update this question with something else?

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

I'm currently trying to design a simple browser app utilizing express. console.log(req.body) comes back {} and i cant find the solution, been here for the best part of the day lol

Here's my app

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I changed the parameter of the input tag from "id" to "name" and it's working fine now.

original

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

new

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

thankx guys


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...