Unable to fetch html hidden input values into express app

◇◆丶佛笑我妖孽 提交于 2020-06-01 05:06:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!