Advertisement
Guest User

script ajax html

a guest
Sep 28th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.76 KB | None | 0 0
  1. <script>
  2. $(document).ready(function () {
  3. $.ajax({
  4. url: 'http://localhost:8084/mavenCarrito/session?ob=usuario&op=check',
  5. type: 'GET',
  6. xhrFields: {
  7. withCredentials: true
  8. },
  9. datatype: 'json',
  10. success: function (data) {
  11. //si existe una sesion, se esconde el formulario de login.
  12. if (data.user != null) {
  13. visualizarSesion();
  14. $("#elh2").empty().append("<h2>Bienvenido: " + data.user + "!</h2>");
  15. $.ajax({
  16. url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
  17. type: 'GET',
  18. xhrFields: {
  19. withCredentials: true
  20. },
  21. datatype: 'json',
  22. success: function (data) {
  23. if (data.status == 200) {
  24. for (i = 0; i < data.almacen.length; i++) {
  25. $("#nombre" + (i + 1)).append(data.almacen[i].Nombre);
  26. $("#precio" + (i + 1)).append(data.almacen[i].Precio);
  27. $("#stock" + (i + 1)).append(data.almacen[i].Stock);
  28. $("#img" + (i + 1)).attr("src", data.almacen[i].img);
  29. }
  30. }
  31.  
  32. },
  33. error: function () {
  34. $.ajax({
  35. url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list',
  36. type: 'GET',
  37. xhrFields: {
  38. withCredentials: true
  39. },
  40. datatype: 'json',
  41. success: function (data) {
  42. if (data.status == 200) {
  43. for (i = 0; i < data.almacen.length; i++) {
  44. $("#nombre" + (i + 1)).append(data.almacen[i].Nombre);
  45. $("#precio" + (i + 1)).append(data.almacen[i].Precio);
  46. $("#stock" + (i + 1)).append(data.almacen[i].Stock);
  47. $("#img" + (i + 1)).attr("src", data.almacen[i].img);
  48. }
  49. }
  50.  
  51. }
  52. });
  53. }
  54. });
  55. $.ajax({
  56. url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=list',
  57. type: 'GET',
  58. xhrFields: {
  59. withCredentials: true
  60. },
  61. datatype: 'json',
  62. success: function (data) {
  63. for (i = 0; i < data.carrito.length; i++) {
  64. $("#cart" + (i + 1)).empty().html(data.carrito[i].Nombre);
  65. $("#qty" + (i + 1)).empty().append(data.carrito[i].Stock);
  66. $("#price" + (i + 1)).empty().append(data.carrito[i].Precio);
  67. $("#price_sub" + (i + 1)).empty().append((data.carrito[i].Stock) * (data.carrito[i].Precio));
  68.  
  69. }
  70. }
  71. });
  72.  
  73.  
  74.  
  75. } else {
  76. visualizarLogin();
  77. }
  78. },
  79. error: function () {
  80. $("#errores").html("<p>Error.</p>");
  81. }
  82. });
  83.  
  84. $("#loguear").click(function () {
  85. event.preventDefault();
  86. nomUser = $("#user").val();
  87. nomPass = $("#pass").val();
  88. //function asd(nomUser, nomPass) {
  89. $.ajax({
  90. url: 'http://localhost:8084/mavenCarrito/session?ob=usuario&op=login&user=' + nomUser + '&pass=' + nomPass,
  91. type: 'GET',
  92. xhrFields: {
  93. withCredentials: true
  94. },
  95. datatype: 'json',
  96. success: function (data) {
  97.  
  98. if (data.status == 200) {
  99. visualizarSesion();
  100. $("#elh2").empty().append("<h2>Bienvenido: " + data.user + "!</h2>");
  101. /* $.ajax({
  102. url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list',
  103. type: 'GET',
  104. xhrFields: {
  105. withCredentials: true
  106. },
  107. datatype: 'json',
  108. success: function (data) {
  109. if (data.status == 200) {
  110. for (i = 0; i < data.almacen.length; i++) {
  111. $("#nombre" + (i + 1)).append(data.almacen[i].Nombre);
  112. $("#precio" + (i + 1)).append(data.almacen[i].Precio);
  113. $("#stock" + (i + 1)).append(data.almacen[i].Stock);
  114. $("#img" + (i + 1)).attr("src", data.almacen[i].img);
  115. }
  116. }
  117.  
  118. }
  119. });*/
  120. } else {
  121. $("#errores").html('<p>Error, revise la contrasenya</p>');
  122. }
  123. },
  124. error: function () {
  125. $("#errores").html("<p>Error.</p>");
  126. }
  127. });
  128. //}
  129. //asd(nomUser, nomPass);
  130. });
  131.  
  132. $("#logout").click(function () {
  133. event.preventDefault();
  134. $.ajax({
  135. url: 'http://localhost:8084/mavenCarrito/session?ob=usuario&op=logout',
  136. type: 'GET',
  137. xhrFields: {
  138. withCredentials: true
  139. },
  140. datatype: 'json',
  141. success: function (data) {
  142. if (data.status == 200) {
  143. visualizarLogin();
  144. $("#adios").html("<h3>Has sido desconectado correctamente</h3>");
  145. }
  146. },
  147. error: function () {
  148. $("#errores").html("<p>Error.</p>");
  149. }
  150. });
  151. });
  152.  
  153. /*for (i = 0; i < 4; i++) {
  154. $(a)[i].click(function () {
  155. event.preventDefault();
  156. cant = $("#inp"+(i+1)).val();
  157. });
  158. }*/
  159.  
  160. //----------------------- BOTONERA -----------------------------------------
  161. //-----------------------BOTON 1 -----------------------------------------
  162. $("#boton1").click(function () {
  163. event.preventDefault();
  164. cant = $("#inp1").val();
  165. $.ajax({
  166. url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=add&prod=1&cant=' + cant,
  167. type: 'GET',
  168. xhrFields: {
  169. withCredentials: true
  170. },
  171. datatype: 'json',
  172. success: function (data) {
  173. $("#cart1").empty().html(data.carrito[0].Nombre);
  174. $("#qty1").empty().html(data.carrito[0].Stock);
  175. $("#price1").empty().html(data.carrito[0].Precio);
  176. $("#price_sub1").empty().html((data.carrito[0].Stock) * (data.carrito[0].Precio));
  177. }
  178. });
  179. $.ajax({
  180. url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
  181. type: 'GET',
  182. xhrFields: {
  183. withCredentials: true
  184. },
  185. datatype: 'json',
  186. success: function (data) {
  187. if (data.status == 200) {
  188. for (i = 0; i < data.almacen.length; i++) {
  189. $("#nombre" + (i + 1)).empty().append(data.almacen[i].Nombre);
  190. $("#precio" + (i + 1)).empty().append(data.almacen[i].Precio);
  191. $("#stock" + (i + 1)).empty().append(data.almacen[i].Stock);
  192. $("#img" + (i + 1)).empty().attr("src", data.almacen[i].img);
  193. }
  194. }
  195.  
  196. }
  197. });
  198. });
  199.  
  200.  
  201. //-----------------------BOTON 2 -----------------------------------------
  202. $("#boton2").click(function () {
  203. event.preventDefault();
  204. cant = $("#inp2").val();
  205. $.ajax({
  206. url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=add&prod=2&cant=' + cant,
  207. type: 'GET',
  208. xhrFields: {
  209. withCredentials: true
  210. },
  211. datatype: 'json',
  212. success: function (data) {
  213. $("#cart2").empty().html(data.carrito[1].Nombre);
  214. $("#qty2").empty().append(data.carrito[1].Stock);
  215. $("#price2").empty().append(data.carrito[1].Precio);
  216. $("#price_sub2").empty().append((data.carrito[1].Stock) * (data.carrito[1].Precio));
  217. }
  218. });
  219. $.ajax({
  220. url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
  221. type: 'GET',
  222. xhrFields: {
  223. withCredentials: true
  224. },
  225. datatype: 'json',
  226. success: function (data) {
  227. if (data.status == 200) {
  228. for (i = 0; i < data.almacen.length; i++) {
  229. $("#nombre" + (i + 1)).empty().append(data.almacen[i].Nombre);
  230. $("#precio" + (i + 1)).empty().append(data.almacen[i].Precio);
  231. $("#stock" + (i + 1)).empty().append(data.almacen[i].Stock);
  232. $("#img" + (i + 1)).empty().attr("src", data.almacen[i].img);
  233. }
  234. }
  235.  
  236. }
  237. });
  238. });
  239.  
  240. //-----------------------BOTON 3 -----------------------------------------
  241. $("#boton3").click(function () {
  242. event.preventDefault();
  243. cant = $("#inp3").val();
  244. $.ajax({
  245. url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=add&prod=3&cant=' + cant,
  246. type: 'GET',
  247. xhrFields: {
  248. withCredentials: true
  249. },
  250. datatype: 'json',
  251. success: function (data) {
  252. $("#cart3").empty().html(data.carrito[2].Nombre);
  253. $("#qty3").empty().append(data.carrito[2].Stock);
  254. $("#price3").empty().append(data.carrito[2].Precio);
  255. $("#price_sub3").empty().append((data.carrito[2].Stock) * (data.carrito[2].Precio));
  256. }
  257. });
  258. $.ajax({
  259. url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
  260. type: 'GET',
  261. xhrFields: {
  262. withCredentials: true
  263. },
  264. datatype: 'json',
  265. success: function (data) {
  266. if (data.status == 200) {
  267. for (i = 0; i < data.almacen.length; i++) {
  268. $("#nombre" + (i + 1)).empty().append(data.almacen[i].Nombre);
  269. $("#precio" + (i + 1)).empty().append(data.almacen[i].Precio);
  270. $("#stock" + (i + 1)).empty().append(data.almacen[i].Stock);
  271. $("#img" + (i + 1)).empty().attr("src", data.almacen[i].img);
  272. }
  273. }
  274.  
  275. }
  276. });
  277. });
  278.  
  279. //-----------------------BOTON 4 -----------------------------------------
  280. $("#boton4").click(function () {
  281. event.preventDefault();
  282. cant = $("#inp4").val();
  283. $.ajax({
  284. url: 'http://localhost:8084/mavenCarrito/session?ob=carrito&op=add&prod=4&cant=' + cant,
  285. type: 'GET',
  286. xhrFields: {
  287. withCredentials: true
  288. },
  289. datatype: 'json',
  290. success: function (data) {
  291. $("#cart4").empty().html(data.carrito[3].Nombre);
  292. $("#qty4").empty().append(data.carrito[3].Stock);
  293. $("#price4").empty().append(data.carrito[3].Precio);
  294. $("#price_sub4").empty().append((data.carrito[3].Stock) * (data.carrito[3].Precio));
  295. }
  296. });
  297. $.ajax({
  298. url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list&act=act',
  299. type: 'GET',
  300. xhrFields: {
  301. withCredentials: true
  302. },
  303. datatype: 'json',
  304. success: function (data) {
  305. if (data.status == 200) {
  306. for (i = 0; i < data.almacen.length; i++) {
  307. $("#nombre" + (i + 1)).empty().append(data.almacen[i].Nombre);
  308. $("#precio" + (i + 1)).empty().append(data.almacen[i].Precio);
  309. $("#stock" + (i + 1)).empty().append(data.almacen[i].Stock);
  310. $("#img" + (i + 1)).empty().attr("src", data.almacen[i].img);
  311. }
  312. }
  313. }
  314. });
  315. });
  316.  
  317.  
  318. function visualizarSesion() {
  319. $("#fueraForm").css("display", "none");
  320. $("#fueraForm").css("visibility", "hidden");
  321. $("#teveo").css("visibility", "visible");
  322. $("#errores").empty();
  323. $("#adios").empty();
  324. }
  325.  
  326. function visualizarLogin() {
  327. $("#fueraForm").css("display", "block");
  328. $("#fueraForm").css("visibility", "visible");
  329. $("#teveo").css("visibility", "hidden");
  330. $("#elh2").empty();
  331. $("#errores").empty();
  332. }
  333.  
  334. /*$.ajax({
  335. url: 'http://localhost:8084/mavenCarrito/session?ob=almacen&op=list',
  336. type: 'GET',
  337. xhrFields: {
  338. withCredentials: true
  339. },
  340. datatype: 'json',
  341. success: function (data) {
  342. if (data.status == 200) {
  343. for (i = 0; i < data.almacen.length; i++) {
  344. $("#nombre" + (i + 1)).append(data.almacen[i].Nombre);
  345. $("#precio" + (i + 1)).append(data.almacen[i].Precio);
  346. $("#stock" + (i + 1)).append(data.almacen[i].Stock);
  347. $("#img" + (i + 1)).attr("src", data.almacen[i].img);
  348. }
  349. }
  350.  
  351. }
  352. });*/
  353. });
  354. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement