简体   繁体   中英

How to retrieve data/key:value pairs from URL-encoded form using POST in express/node.js?

I have a html from that have 4 input fields. Form is configured like this:

<form action="/" method="post" onsubmit="alert('data submittet')">

So, it should default to x-www-form-urlencoded. The form is working, and i can submit my data.

On the server/node.js side i have the following configration and code:

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({
extended: true
}));


app.post("/", function (req, res, next) {

console.log(req.body);

console.log(req.body.var1);
console.log(typeof(req.body.var1));
});

What i get from this is is the actual values in curly braces in my console window:

{ var1: '123456', text1: 'oiu', text2: 'abc', text3: 'def' }

But i can not console.log the individual parameters as they are 'undefined' and i do not understand how to get these values so i can use them in the code on the server side. Am i missing some parser, setting or why should this not be working?

Oh gee. It was just to store the req.body data in a variable :S

app.post("/", function (req, res, next) { 
console.log(req.body); 
var receiveddata = req.body; 
console.log(receiveddata.var1); 
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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