i need to check if a token entred by user exist ,
my node js controller :
const checktoken = async (req, res) => {
const token = req.body.token
User.findOne({'token': token }).then((user) => {
if (user) {
return res.end("GOOOOD");
}
return res.end("Baad");
})
.catch((err) => console.log(err))
}
Ajax side :
$(document).ready(function(){
$('#form').submit(function (e) {
// e.preventDefault()
$.ajax({
type: 'POST',
url: '/api/token/'
})
.done(function(data) {
alert(data)
})
.fail(function(xhr, status, error) {
console.log("Errrrrrr " + error);
})
.always(function(data){
});
})
})
html form :
<form class="form-inline search-form" action ="/api/token" method="POST" id="form">
<div class="form-group label-floating">
<input name ="token" id="token" >
</div>
<button id="subBut" type ="submit"> Verify </button>
</form>
the problem is i got always an alert (BAD token) whenever is good or not and then another page is printed with good result in text format
exemple if i put good token : Alert is Bad token then another page printed : good token (this means result from node is ok) the problem with AJAX
req.body? I don't see anywhere that you're sending the token. Also, are you using bodyparser?If you need to respond with data, instead use methods such as res.send() and res.json().