我正在创建一个带有Nodejs服务器的web平台。我正在尝试检索从我的前端发送的urlencoded数据,但无法实现。
如何发送GET请求:
xhr.open("GET", address + "?limit=1&offset=1",true);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(null);
xhr.addEventListener("readystatechange", processRequest, false);在服务器端:
const bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: true });
app.get('/guid_list', urlencodedParser, function (req, res) {
console.log(req.body.limit);
console.log(req.body.offset);
var headerjwt = HeaderGetJWT(req);
...
}检索我发送的jwt令牌没有问题,但urlencoded参数总是没有定义。我想知道我是否应该使用多部分内容类型,因为我同时发送令牌和urlencoded数据?在这种情况下可能还有"multer“模块,因为body-Parser不支持该内容类型。
发布于 2019-10-31 19:31:53
我建议按如下方式访问Node.js中的参数(因为它们是作为查询参数传递的):
app.get('/guid_list', parser, function (req, res) {
console.log("req.query.limit:", req.query.limit);
console.log("req.query.offset:", req.query.offset);
});或者只记录所有参数:
app.get('/guid_list', parser, function (req, res) {
console.log("req.query:", req.query);
});https://stackoverflow.com/questions/58641488
复制相似问题