Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- $(document).ready(function () {
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=usuario&op=check',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- //si existe una sesion, se esconde el formulario de login.
- if (data.user != null) {
- visualizarSesion();
- $("#elh2").empty().append("<h2>Bienvenido: " + data.user + "!</h2>");
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- for (i = 0; i < data.almacen.length; i++) {
- $("#nombre" + (i + 1)).append(data.almacen[i].Nombre);
- $("#precio" + (i + 1)).append(data.almacen[i].Precio);
- $("#stock" + (i + 1)).append(data.almacen[i].Stock);
- $("#img" + (i + 1)).attr("src", data.almacen[i].img);
- }
- }
- },
- error: function () {
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- for (i = 0; i < data.almacen.length; i++) {
- $("#nombre" + (i + 1)).append(data.almacen[i].Nombre);
- $("#precio" + (i + 1)).append(data.almacen[i].Precio);
- $("#stock" + (i + 1)).append(data.almacen[i].Stock);
- $("#img" + (i + 1)).attr("src", data.almacen[i].img);
- }
- }
- }
- });
- }
- });
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=list',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- for (i = 0; i < data.carrito.length; i++) {
- $("#cart" + (i + 1)).empty().html(data.carrito[i].Nombre);
- $("#qty" + (i + 1)).empty().append(data.carrito[i].Stock);
- $("#price" + (i + 1)).empty().append(data.carrito[i].Precio);
- $("#price_sub" + (i + 1)).empty().append((data.carrito[i].Stock) * (data.carrito[i].Precio));
- }
- }
- });
- } else {
- visualizarLogin();
- }
- },
- error: function () {
- $("#errores").html("<p>Error.</p>");
- }
- });
- $("#loguear").click(function () {
- event.preventDefault();
- nomUser = $("#user").val();
- nomPass = $("#pass").val();
- //function asd(nomUser, nomPass) {
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=usuario&op=login&user=' + nomUser + '&pass=' + nomPass,
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- visualizarSesion();
- $("#elh2").empty().append("<h2>Bienvenido: " + data.user + "!</h2>");
- /* $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- for (i = 0; i < data.almacen.length; i++) {
- $("#nombre" + (i + 1)).append(data.almacen[i].Nombre);
- $("#precio" + (i + 1)).append(data.almacen[i].Precio);
- $("#stock" + (i + 1)).append(data.almacen[i].Stock);
- $("#img" + (i + 1)).attr("src", data.almacen[i].img);
- }
- }
- }
- });*/
- } else {
- $("#errores").html('<p>Error, revise la contrasenya</p>');
- }
- },
- error: function () {
- $("#errores").html("<p>Error.</p>");
- }
- });
- //}
- //asd(nomUser, nomPass);
- });
- $("#logout").click(function () {
- event.preventDefault();
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=usuario&op=logout',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- visualizarLogin();
- $("#adios").html("<h3>Has sido desconectado correctamente</h3>");
- }
- },
- error: function () {
- $("#errores").html("<p>Error.</p>");
- }
- });
- });
- /*for (i = 0; i < 4; i++) {
- $(a)[i].click(function () {
- event.preventDefault();
- cant = $("#inp"+(i+1)).val();
- });
- }*/
- //----------------------- BOTONERA -----------------------------------------
- //-----------------------BOTON 1 -----------------------------------------
- $("#boton1").click(function () {
- event.preventDefault();
- cant = $("#inp1").val();
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=add&prod=1&cant=' + cant,
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- $("#cart1").empty().html(data.carrito[0].Nombre);
- $("#qty1").empty().html(data.carrito[0].Stock);
- $("#price1").empty().html(data.carrito[0].Precio);
- $("#price_sub1").empty().html((data.carrito[0].Stock) * (data.carrito[0].Precio));
- }
- });
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- for (i = 0; i < data.almacen.length; i++) {
- $("#nombre" + (i + 1)).empty().append(data.almacen[i].Nombre);
- $("#precio" + (i + 1)).empty().append(data.almacen[i].Precio);
- $("#stock" + (i + 1)).empty().append(data.almacen[i].Stock);
- $("#img" + (i + 1)).empty().attr("src", data.almacen[i].img);
- }
- }
- }
- });
- });
- //-----------------------BOTON 2 -----------------------------------------
- $("#boton2").click(function () {
- event.preventDefault();
- cant = $("#inp2").val();
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=add&prod=2&cant=' + cant,
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- $("#cart2").empty().html(data.carrito[1].Nombre);
- $("#qty2").empty().append(data.carrito[1].Stock);
- $("#price2").empty().append(data.carrito[1].Precio);
- $("#price_sub2").empty().append((data.carrito[1].Stock) * (data.carrito[1].Precio));
- }
- });
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- for (i = 0; i < data.almacen.length; i++) {
- $("#nombre" + (i + 1)).empty().append(data.almacen[i].Nombre);
- $("#precio" + (i + 1)).empty().append(data.almacen[i].Precio);
- $("#stock" + (i + 1)).empty().append(data.almacen[i].Stock);
- $("#img" + (i + 1)).empty().attr("src", data.almacen[i].img);
- }
- }
- }
- });
- });
- //-----------------------BOTON 3 -----------------------------------------
- $("#boton3").click(function () {
- event.preventDefault();
- cant = $("#inp3").val();
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=add&prod=3&cant=' + cant,
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- $("#cart3").empty().html(data.carrito[2].Nombre);
- $("#qty3").empty().append(data.carrito[2].Stock);
- $("#price3").empty().append(data.carrito[2].Precio);
- $("#price_sub3").empty().append((data.carrito[2].Stock) * (data.carrito[2].Precio));
- }
- });
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- for (i = 0; i < data.almacen.length; i++) {
- $("#nombre" + (i + 1)).empty().append(data.almacen[i].Nombre);
- $("#precio" + (i + 1)).empty().append(data.almacen[i].Precio);
- $("#stock" + (i + 1)).empty().append(data.almacen[i].Stock);
- $("#img" + (i + 1)).empty().attr("src", data.almacen[i].img);
- }
- }
- }
- });
- });
- //-----------------------BOTON 4 -----------------------------------------
- $("#boton4").click(function () {
- event.preventDefault();
- cant = $("#inp4").val();
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=add&prod=4&cant=' + cant,
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- $("#cart4").empty().html(data.carrito[3].Nombre);
- $("#qty4").empty().append(data.carrito[3].Stock);
- $("#price4").empty().append(data.carrito[3].Precio);
- $("#price_sub4").empty().append((data.carrito[3].Stock) * (data.carrito[3].Precio));
- }
- });
- $.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- for (i = 0; i < data.almacen.length; i++) {
- $("#nombre" + (i + 1)).empty().append(data.almacen[i].Nombre);
- $("#precio" + (i + 1)).empty().append(data.almacen[i].Precio);
- $("#stock" + (i + 1)).empty().append(data.almacen[i].Stock);
- $("#img" + (i + 1)).empty().attr("src", data.almacen[i].img);
- }
- }
- }
- });
- });
- function visualizarSesion() {
- $("#fueraForm").css("display", "none");
- $("#fueraForm").css("visibility", "hidden");
- $("#teveo").css("visibility", "visible");
- $("#errores").empty();
- $("#adios").empty();
- }
- function visualizarLogin() {
- $("#fueraForm").css("display", "block");
- $("#fueraForm").css("visibility", "visible");
- $("#teveo").css("visibility", "hidden");
- $("#elh2").empty();
- $("#errores").empty();
- }
- /*$.ajax({
- url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list',
- type: 'GET',
- xhrFields: {
- withCredentials: true
- },
- datatype: 'json',
- success: function (data) {
- if (data.status == 200) {
- for (i = 0; i < data.almacen.length; i++) {
- $("#nombre" + (i + 1)).append(data.almacen[i].Nombre);
- $("#precio" + (i + 1)).append(data.almacen[i].Precio);
- $("#stock" + (i + 1)).append(data.almacen[i].Stock);
- $("#img" + (i + 1)).attr("src", data.almacen[i].img);
- }
- }
- }
- });*/
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement