问题
I'm trying to fetch some hidden form values in post route of my express app,but getting undefined objects in post route,below is some code snippet
const express = require("express")
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.listen(3000, () => {
console.log('Authentication service started on port 3000');
});
app.get("/link",(req,res)=>{
let token = 124578
let link="http://"+req.get('host')+"/postmethod";
console.log("hyperlink:"+link)
let formHtmlPostLink="<form method='post' action='"+link+"'> <input type='hidden' name='bearer' value='"+token+"'> <input type='hidden' name='test' value='test'> <button type='submit' name='submit_param' value='submit_value' class='link-button'> Reset Password </button> </form>"
res.send(formHtmlPostLink)
})
app.post("/postmethod",(req,res)=>{
res.send(req.body)//undefined objects
})
回答1:
If you are able to submit form to /postmethod
, add middleware bodyParser.urlencoded()
app.use(bodyParser.urlencoded());
If form is not being submitted, change link to relative url
let link = "/postmethod";
Check codesandbox
来源:https://stackoverflow.com/questions/62064535/unable-to-fetch-html-hidden-input-values-into-express-app