这是我的简单表单:
<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>
这是我的Express.js/Node.js代码:
app.post('/userlogin', function(sReq, sRes){
var email = sReq.query.email.;
}
我试过sReq。query。email或sReq。查询['email']或sReq。params['邮件'],等等。没有一个有用。它们都返回undefined。
当我更改为Get调用时,它可以工作,所以。任何想法?
给定某种形式:
<form action='/somepath' method='post'>
<input type='text' name='name'></input>
</form>
使用快递
app.post('/somepath', function(req, res) {
console.log(JSON.stringify(req.body));
console.log('req.body.name', req.body['name']);
});
输出:
{"name":"x","description":"x"}
req.param.name x
后端:
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
app.use(bodyParser.json()); // add a middleware (so that express can parse request.body's json)
app.post('/api/courses', (request, response) => {
response.json(request.body);
});
前端:
fetch("/api/courses", {
method: 'POST',
body: JSON.stringify({ hi: 'hello' }), // convert Js object to a string
headers: new Headers({ "Content-Type": "application/json" }) // add headers
});