无法将HTML隐藏的输入值提取到Express应用中 [英] Unable to fetch html hidden input values into express app

查看:39
本文介绍了无法将HTML隐藏的输入值提取到Express应用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Express应用程序的发布路径中获取一些隐藏的表单值,但是在发布路径中获取未定义的对象,下面是一些代码段

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
})

推荐答案

如果您能够将表单提交到/postmethod ,请添加中间件 bodyParser.urlencoded()

If you are able to submit form to /postmethod, add middleware bodyParser.urlencoded()

app.use(bodyParser.urlencoded());

如果未提交表单,请将链接更改为相对URL

If form is not being submitted, change link to relative url

  let link = "/postmethod";

检查 codesandbox

这篇关于无法将HTML隐藏的输入值提取到Express应用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆