login.js 980 Bytes
function login() {
	message("Espere...");
	const usr = document.getElementsByName("usuario")[0];
	const psw = document.getElementsByName("contrasena")[0];

	function BadInputException(input) {
		this.error = "badinput";
		this.input = input;
	}

	if (usr.checkValidity() === false) {
		usr.reportValidity("Por favor llene este campo");
		throw BadInputException(input);
	}

	if (psw.checkValidity() === false) {
		psw.reportValidity("Por favor llene este campo");
		throw BadInputException(input);
	}

	const obj = {
		"url": "/login",
		"contentType": "application/json",
		"async": true,
		"data": { "usuario" : usr.value, "contrasena" : psw.value },
		"success": (response) => {
			console.log(response)
			if (response === "true")
				location.href = "/";
			else
				message(response);
		},
		"error": (response) => message(response),
		"timeout": 120000,
		"ontimeout": (response) => message("La conexión tardó demasiado.")
	}

	const ajax = new Ajax(obj);
	ajax.post();
}