【发布时间】:2016-06-12 09:16:39
【问题描述】:
我正在尝试向接受 post/put 调用的节点 JS 服务器发出请求。我试图通过 chai 通过 post 调用发送的参数在服务器 (req.body.myparam) 上不可见。
我尝试了以下发布请求,但没有结果:-
var host = "http://localhost:3000";
var path = "/myPath";
chai.request(host).post(path).field('myparam' , 'test').end(function(error, response, body) {
和
chai.request(host).post(path).send({'myparam' : 'test'}).end(function(error, response, body) {
Node JS 代码如下:-
app.put ('/mypath', function(req, res){ //Handling post request to create league
createDoc (req, res);
})
app.post ('/mypath', function(req, res){ //Handling post request to create league
createDoc (req, res);
})
var createDoc = function (req, res) {
var myparam = req.body.myparam; //league id to create new league
if (!myparam) {
res.status(400).json({error : 'myparam is missing'});
return;
}
};
上面的代码到 myparam 丢失了。
请告诉我最好的方法是什么。
提前致谢。
【问题讨论】:
-
可以分享端点的代码吗?
-
更新了代码。如果您还需要什么,请告诉我。
-
我没有看到
league在任何地方定义? -
我的错误。没有正确复制代码。更新了上面的代码。请检查。谢谢