如何访问 POST 表单字段 [英] How to access POST form fields

查看:41
本文介绍了如何访问 POST 表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的简单表格:

<div><label for="email">电子邮件:</label><input type="text" id="email" name="email"></input>

<input type="submit" value="提交"></input></表单>

这是我的 Express.js/Node.js 代码:

app.post('/userlogin', function(sReq, sRes){var email = sReq.query.email.;}

我尝试了 sReq.query.emailsReq.query['email']sReq.params['email'],等等.它们都不起作用.它们都返回 undefined.

当我更改为 Get 调用时,它会起作用,所以.. 知道吗?

解决方案

事情有 Express 4.16.0 再次更改,您现在可以使用 express.json()express.urlencoded()就像在 Express 3.0 中一样.

这是不同的,从Express 4.0 到 4.15:

$ npm install --save body-parser

然后:

var bodyParser = require('body-parser')app.use(bodyParser.json());//支持 JSON 编码的主体app.use(bodyParser.urlencoded({//支持 URL 编码的主体扩展:真实}));

其余的就像 Express 3.0:

首先需要添加一些中间件来解析body的post数据.

添加以下一行或两行代码:

app.use(express.json());//支持 JSON 编码的主体app.use(express.urlencoded());//支持 URL 编码的主体

然后,在您的处理程序中,使用 req.body 对象:

//假设POST:name=foo&color=red <-- URL编码////或 POST: {"name":"foo","color":"red"} <-- JSON 编码app.post('/test-page', function(req, res) {var name = req.body.name,颜色 = req.body.color;//...});


注意 express.bodyParser() 的使用是不推荐.

app.use(express.bodyParser());

...相当于:

app.use(express.json());app.use(express.urlencoded());app.use(express.multipart());

express.multipart() 存在安全问题,因此最好明确添加对您需要的特定编码类型的支持.如果您确实需要多部分编码(例如支持上传文件),那么您应该 阅读本文.

Here is my simple form:

<form id="loginformA" action="userlogin" method="post">
    <div>
        <label for="email">Email: </label>
        <input type="text" id="email" name="email"></input>
    </div>
<input type="submit" value="Submit"></input>
</form>

Here is my Express.js/Node.js code:

app.post('/userlogin', function(sReq, sRes){    
    var email = sReq.query.email.;   
}

I tried sReq.query.email or sReq.query['email'] or sReq.params['email'], etc. None of them work. They all return undefined.

When I change to a Get call, it works, so .. any idea?

解决方案

Things have changed once again starting Express 4.16.0, you can now use express.json() and express.urlencoded() just like in Express 3.0.

This was different starting Express 4.0 to 4.15:

$ npm install --save body-parser

and then:

var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 

The rest is like in Express 3.0:

Firstly you need to add some middleware to parse the post data of the body.

Add one or both of the following lines of code:

app.use(express.json());       // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies

Then, in your handler, use the req.body object:

// assuming POST: name=foo&color=red            <-- URL encoding
//
// or       POST: {"name":"foo","color":"red"}  <-- JSON encoding

app.post('/test-page', function(req, res) {
    var name = req.body.name,
        color = req.body.color;
    // ...
});


Note that the use of express.bodyParser() is not recommended.

app.use(express.bodyParser());

...is equivalent to:

app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());

Security concerns exist with express.multipart(), and so it is better to explicitly add support for the specific encoding type(s) you require. If you do need multipart encoding (to support uploading files for example) then you should read this.

这篇关于如何访问 POST 表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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