我有一个具有输入数据的ajax get请求。我想将这些数据传递给一个变量并在我的查询中使用它。将ajax值传递给node.js服务器中的变量
index.handlebars
$(document).ready(function() {
var username = $('.test').val();
$.ajax({
type : 'GET',
url: '/users//welcome',
data : {
wew: username
}
});
alert(username); //correct output
});
,并在我的用户/欢迎
router.get('/welcome', ensureAuthenticated, function(req, res){
var test = req.query.wew; //data from ajax request
Item.find({username: test},function(err, docs, test){
//where username is the data
console.log(test);
res.render('welcome', {docs:docs});
});
});
此代码不能正常工作。请帮忙:/
+0
“的代码是不工作”是非常广泛 - 你能解释一下你的问题吗 –
+0
我想将ajax数据存储到server.js中的一个变量中。第二组代码是我的服务器 –
+0
,因此您需要将数据从浏览器发送到服务器? –