Answer the question
In order to leave comments, you need to log in
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);
}
})
});
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;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question