入力のデータを持つajax getリクエストがあります。私はこのデータを変数に渡して、私のクエリでそれを使用したいと思います。node.jsサーバーの変数にajax値を渡します。
index.handlebars
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="hidden" class="test" name="test" value="sample"/>
<script type="text/javascript">
$(document).ready(function() {
var username = $('.test').val();
$.ajax({
type : 'GET',
url: '/users//welcome',
data : {
wew: username
}
});
alert(username); //correct output
});
</script>
</body>
</html>
と私のユーザーが/は、このコードは動作していない
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});
});
});
を歓迎します。助けてください:/
下に試すことができます - あなたの問題が何であるかを説明できますか? –
server.jsの変数にajaxデータを保存します。 2番目のコードセットは私のサーバー –
なので、ブラウザからサーバーにデータを送信する必要がありますか? –