N
N
narem2020-02-12 16:29:41
JavaScript
narem, 2020-02-12 16:29:41

How to track down an error when sending ajax or why the request is not sent?

$('.reg-button').on('click',function(){
  var data = {
    name: $('#reg-login').val(),
    email: $('#reg-email').val(),
    password: $('#reg-password').val(),
    repassword: $('#reg-re-password').val()
    },
    validmail = /^[a-z0-9_-][email protected][a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$/i;
  if($.trim(data.name) == '' || $.trim(data.email) == '' || $.trim(data.password) == '' || $.trim(data.repassword) == ''){
    $('.message-register').text('Одно или несколько полей не заполнены');
  }else if(data.password != data.repassword){
    $('.message-register').text('Пароли не совпадают');
  }else if(data.email.search(validmail) == -1){
    $('.message-register').text('Проверьте правильность почты');
  }else{
    $.ajax({
      type: 'POST',
      data: JSON.stringify(data),
      contentType: 'application/json',
      url: '/api/auth/register'
    }).done(function(data){
      if(!data.ok){
        $('.message-register').text(data.error);
      }
    })
  }
});

$('#in').on('click',function(){
  var data = {
    login: $('#login').val(),
    password: $('#password').val()
    };
    console.log(data);
  $.ajax({
    type: 'POST',
    data: JSON.stringify(data),
    contentType: 'application/json',
    url: '/api/auth/login'
  }).done(function(data){
    if(!data.ok){
      $('.message-register').text(data.error);
    }
  })
});

The fragment with $('.reg-button').on works fine, but when I do it, but when it comes to $('#in').on('click', - error 500
Route Contents falls in the browser console log
const express = require('express'),
  router = express.Router(),
  bcrypt = require('bcrypt-nodejs'),
  models = require('../models');

//Регистрация
router.post('/register', (request, response)=>{
  const login = request.body.name,
    mail = request.body.email,
    pass = request.body.password,
    repass = request.body.repassword;
  if(!login || !mail || !pass || !repass){
    response.json({
      ok: false,
      error: 'Все поля должны быть заполнены!',
      fields: ['login', 'mail', 'pass', 'repass']
    });
  }else if(login.length < 3 || login.length > 16){
    response.json({
      ok: false,
      error: 'Длина логина от 3 до 16 символов!',
      fields: ['login']
    });
  }else if(pass !== repass){
    response.json({
      ok: false,
      error: 'Не совпадают пароль',
      fields: ['pass', 'repass']
    })
  }else{
    models.User.Users.findOne({where: {login:login}})
    .then(user=>{
      if(!user){
        bcrypt.hash(pass, null, null, function(err, hash){
          models.User.Users.create({
            login: login,
            mail: mail,
            password: hash,
            privilege: 1
          }).then(user => {
            response.json({
              ok:true
            })
          }).catch(err => {
            console.log(err);
          })
        }) 
      }else{
        response.json({
          ok: false,
          error: 'Имя занято!',
          fields: ['login']
        })
      }
    })
  }
});


//Авторизация
router.post('/login', (request, response)=>{
  сonsole.log(request.body);
})

module.exports = router;

that is, in fact, everything is the same, but for some reason it does not work in the case of login. What could be the problem?

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