Javascript 无法捕获my routes.js中发布的数据
快速申请。我通过ajax发布数据。但是我无法从我的路线捕获数据。以下是我的ajax:Javascript 无法捕获my routes.js中发布的数据,javascript,jquery,ajax,node.js,express,Javascript,Jquery,Ajax,Node.js,Express,快速申请。我通过ajax发布数据。但是我无法从我的路线捕获数据。以下是我的ajax: function Confirme(commentid,status){ $.ajax({ url: '/Management/Comment/Confirm/', type: "POST", data: {"id": commentid,"status": status}
function Confirme(commentid,status){
$.ajax({
url: '/Management/Comment/Confirm/',
type: "POST",
data: {"id": commentid,"status": status}
})
我正在将commentid(345)和状态(0)发送到此函数。(也在firebug中试用)。这是我的路线代码:
app.post('/Management/Comment/Confirm/',function(req, res){
if(!req.session.email){
return res.render(__dirname+"../../../views/management/accessdenied.jade",{
title: 'Dont have permission',
stylesheet: 'accessdenied',
error: 'You are not authorized to access this page'
});
}
console.log('Is id correct:' +req.params.id);
Comment.findByIdAndUpdate( req.params.id, {"status": 1}, function(err, result) {
if (err) console.log(err);
console.log("result:"+result);
Comment.find({},function(err, comments){
console.log("comments: "+comments)
if(! comments.length) {
console.log('no comment');
}
return res.render(__dirname+"/views/commentindex",{
title: 'comment list',
stylesheet: 'commentindex',
comments:comments
});
});
});
});
但此代码始终返回到未定义:
console.log(req.params.id);
我登记了firebug。我想可能是我无法将数据发送到ajax函数。但不是。以下是firebug:
您应该使用或
在路由URL中包含命名参数。例如:
app.post('/post/:id', ...);
^^^ this can be read using `req.params.id`
您必须编写
req.body.id
。
对于重新请求后的its req.body.paramsName,对于GET请求,its
req.query.paramsName
对于GET
它是req.query.paramsName
。