繁体   English   中英

如何将表单更改为 Content-Type:application / json

[英]How to change the form to Content-Type: application / json

您好,我有一个简单的表格,我想通过邮寄方式发送,稍后通过快递库获取,我提交运输状态时的问题是应用程序/x-www-form-urlencoded,我想要内容类型:应用程序/json。 我该如何改变它? 为了理解我,我添加了我的代码。

 <form action="http://localhost:3000/test" method="POST" class="card" > <div><label for="nameinvstement">:שם השקעה</label></div> <div> <input type="text" name="nameInvstement" id="nameinvstement" required /> </div> <button type="submit"></button> <form/>

 app.post("/test", async (req, res) => { const { nameInvstement, } = req.body; try { await collection.insertOne({ nameInvstement, }); res.status(200).json("OK"); } catch (e) { res.send(e); } });

我建议查看app.use ,因为您可以指定如下输出:

解析application/json

app.use(express.json())

解析application/x-www-form-urlencoded

app.use(express.urlencoded({ extended: true }}

示例

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

app.post('/testing', function(req, res) {
    console.log("Start submitting");
    console.log(req.body);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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