本文介绍了express3 js req.body未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用req.body时使用的是express2.js,我得不到定义或为空{}:
I am using express2.js when using req.body I get undefined or empty {}:
exports.post = function (req: express3.Request, res: express3.Response) {
console.log(req.body);
});
我有以下配置:
app.use(express.bodyParser());
app.use(app.router);
app.post('/getuser', routes.getuserprofile.post);
请求正文为XML,我检查了请求标头是否正确.
The request body is in XML and I checked the request header which was correct.
推荐答案
我错过了拥有XML的部分.我想req.body默认不会解析.
I missed the part where you had XML. I guess req.body doesn't parse by default.
如果您使用的是Express 2.x,则@DavidKrisch的此解决方案(复制到下面)
If you are using Express 2.x then perhaps this solution by @DavidKrisch is adequate (copied below)
// This script requires Express 2.4.2
// It echoes the xml body in the request to the response
//
// Run this script like so:
// curl -v -X POST -H 'Content-Type: application/xml' -d '<hello>world</hello>' http://localhost:3000
var express = require('express'),
app = express.createServer();
express.bodyParser.parse['application/xml'] = function(data) {
return data;
};
app.configure(function() {
app.use(express.bodyParser());
});
app.post('/', function(req, res){
res.contentType('application/xml');
res.send(req.body, 200);
});
app.listen(3000);
这篇关于express3 js req.body未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!