0

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

2
  • What's in req.body? I don't see anywhere that you're sending the token. Also, are you using bodyparser? Commented May 14, 2020 at 22:23
  • Assuming you are using express, the docs say If you need to respond with data, instead use methods such as res.send() and res.json(). Commented May 14, 2020 at 22:28

1 Answer 1

1

First of all, you are double submitting because your form will submit while your ajax call will also fire. Change the action on the form to ensure that it won't try to post on its own (alternatively, you can uncomment e.preventDefault in your submit handler). Also, you aren't including any data in your ajax call:

<form class="form-inline search-form" action ="javascript:void(0)" id="form">
    <div class="form-group label-floating">
        <input name ="token" id="token" >
    </div>

   <button id="subBut" type ="submit"> Verify  </button>
</form>
$(document).ready(function(){
    $('#form').submit(function (e) {
    // e.preventDefault()

        $.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: '/api/token/',
            data: JSON.stringify({ token: $('#token').val() })
        })
        .done(function(data) {
            alert(data)
        })
        .fail(function(xhr, status, error) {
            console.log("Errrrrrr       " + error);
        })
        .always(function(data){
        });
    })
})

You haven't included your Express setup code, but this will require the line app.use(Express.json()) or (express.json() depending on what you named the import) in order to parse the body from JSON into an object

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.