W
W
Wasya UK2016-12-24 18:24:54
JavaScript
Wasya UK, 2016-12-24 18:24:54

How to create password and login validation (ajax + nodejs)?

After submitting the data, the page reloads and displays the received data. In this case, it's OK.
How to make it so that if the status is 200, then the page is redirected to '/chat'?

ajax request script

$(document).ready(function() {
  
  $('#loginform').submit(function() {
    var form = $(this);
    var error = false;
    
    if (!error) {
      var data = form.serialize();
      
      $.ajax({
        url: '/login',
        method: 'POST',
        dataType: 'json'
        data: data,
        beforeSend: function(data) {
          form.find('input[type="submit"]').attr('disabled', 'disabled');
        },
        complete: function(data) {
          form.find('input[type="submit"]').prop('disabled', false);
        },
        statusCode: {
          200: function() {
            window.location.href = "/chat";
          },
          403: function(jqXHR) {
            var error = JSON.parse(jqXHR.responseText);
            alert(error.message);
          }
        },
        error: function (xhr, ajaxOptions, thrownError) {
          alert(xhr.status);
          alert(thrownError);
        }
      });
    } 
    
    return false;
  });
  
});


Path check
app.get('/login', function(req, res) {
    res.render('login');
  });
  
  app.post('/login', function(req, res, next) {
    var username = req.body.username;
    var password = req.body.password;
    
    console.log('Name: ' + username);
    console.log('Pass: ' + password);
    
    res.sendStatus(200);
  });
  
  app.get('/chat', function(req, res) {
    res.render('chat');
  });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question