Last active
June 7, 2024 18:48
-
-
Save ggMartinez/8c781c98d910ef9eccd9ff3ae008ac69 to your computer and use it in GitHub Desktop.
Javascript - Login con API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
$("#boton").click(function(){ | |
var marca = $("#marca").val(); | |
var modelo = $("#modelo").val(); | |
var color = $("#color").val(); | |
var potencia = $("#potencia").val(); | |
var cantidad_pilas = $("#cantidad_pilas").val(); | |
var capacidad = $("#capacidad").val(); | |
var data = { | |
"marca": marca, | |
"modelo": modelo, | |
"color": color, | |
"potencia_maxima": potencia, | |
"cantidad_de_pilas": cantidad_pilas, | |
"capacidad": capacidad | |
}; | |
$.ajax({ | |
url: 'http://localhost:8000/api/vapo/', | |
type: 'POST', | |
headers: { | |
"Accept" : "application/json", | |
"Content-Type" : "application/json", | |
}, | |
data: JSON.stringify(data), | |
success: function(data) { | |
alert("Se ha creado correctamente"); | |
}, | |
error: function(data){ | |
alert("No se ha podido crear"); | |
} | |
}); | |
}); | |
}); | |
</script> | |
Marca <input type="text" id="marca" name="marca"> | |
Modelo <input type="text" id="modelo" name="modelo"> | |
Color <input type="text" id="color" name="color"> | |
Potencia <input type="text" id="potencia" name="potencia_maxima"> | |
Cantidad de Pilas <input type="text" id="cantidad_pilas" name="cantidad_de_pilas"> | |
Capacidad <input type="text" id="capacidad" name="capacidad"> | |
<button id="boton">Enviar</button> | |
</body> | |
</html>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
var token = localStorage.getItem("accessToken"); | |
if(token != null) | |
$(location).prop('href', '/principal.html'); | |
$("#btnLogin").click(function(){ | |
var username = $("#username").val(); | |
var password = $("#password").val(); | |
var data = { | |
"username": username, | |
"password": password, | |
"grant_type" : "password", | |
"client_id" : 101, | |
"client_secret" : "OGhqS9Yspa9GSBpfsNEfC3py4AREMS3YVLiYEk6m" | |
} | |
jQuery.ajax({ | |
url: 'http://localhost:8000/oauth/token', | |
type: 'POST', | |
headers: { | |
"Accept" : "application/json", | |
"Content-Type" : "application/json", | |
}, | |
data: data, | |
success: function(data) { | |
localStorage.setItem("accessToken", data.access_token); | |
$(location).prop('href', '/principal.html'); | |
}, | |
error: function(data){ | |
alert("Credenciales invalidas"); | |
} | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Login</h1> | |
<div id="login"> | |
Usuario: <input type="text" name="username" id="username"> <br> | |
Password: <input type="password" name="password" id="password"> <br> | |
<button id="btnLogin">Login</button> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
var token = localStorage.getItem("accessToken"); | |
if(token == null) | |
$(location).prop('href', '/login.html'); | |
$("#cerrarSesion").click(function(){ | |
jQuery.ajax({ | |
url: 'http://localhost:8000/api/logout', | |
type: 'GET', | |
headers: { | |
"Authorization" : "Bearer " + localStorage.getItem("accessToken"), | |
"Accept" : "application/json", | |
"Content-Type" : "application/json", | |
}, | |
success: function(data) { | |
localStorage.removeItem("accessToken"); | |
$(location).prop('href', '/login.html'); | |
} | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Holi</h1> | |
<button id="cerrarSesion">Cerrar sesion</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment