-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
132 lines (117 loc) · 4.89 KB
/
Copy pathindex.html
File metadata and controls
132 lines (117 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Peticiones API</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<body>
<!-- Creamos una tabla simple que nos servira para mostrar de mejor forma nuestros libros-->
<div class="container">
<table id="booksTable" class="table">
<thead>
<tr>
<th>Titulo</th>
<th>Id_Autor</th>
<th>Id_Genero</th>
</tr>
</thead>
<tbody>
</tbody>
<input type="button" value="Cargar libros" id="loadBooks" />
<!-- Aqui creamos un div que nos sirve para almacenar mensajes, con ajax buscamos su primer
elemento que es <p> para introducir texto en el
-->
<div style="display: none;" id="messages">
<p></p>
</div>
<!-- Por defecto mantenemos oculto nuestra tabla con los datos de los libros
hasta que el usuario da click en cargar libros
-->
<div style="display: none;" id="bookForm">
<hr />
<table>
<tr>
<td>Titulo</td>
<td> <input type="text" name="bookTitle" id="bookTitle" /> </td>
</tr>
<tr>
<td>Id Autor</td>
<td> <input type="text" name="bookAuthorId" id="bookAuthorId" /> </td>
</tr>
<tr>
<td>Id Genero</td>
<td> <input type="text" name="bookGenreId" id="bookGenreId" /> </td>
</tr>
<tr>
<td colspan="2"> <input type="button" value="Guardar" id="bookSave"> </td>
</tr>
</table>
</div>
</table>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
<script type="text/javascript">
/*
Iniciamos con nuestra consulta GET simple con los libros
Solo enviamos una peticion y colectamos la data en un arreglo con un for
La cual procesamos en la funcion addBook
*/
$('#loadBooks').click(function () {
$('#messages').first('p').text('Cargando libros...');
$('#messages').show();
$.ajax(
{
'url' : 'http://localhost:8000/books',
'success' : function (data) {
$('#messages').hide();
$('#booksTable > tbody').empty();
for (b in data){
addBook(data[b]);
}
$('#bookForm').show();
}
}
);
});
/*
Con la data proveniente de nuestra consulta GET, vamos construyendo renglones dentro de
la tabla
*/
function addBook(book) {
$('#booksTable tr:last').after('<tr><td>' + book.titulo + '</td><td>' + book.id_autor + '</td><td>' + book.id_genero + '</td></tr>');
}
/*
Otra peticion ahora POST, con ella agarramos los valores de los textField de arriba para
enviarlos dentro de un arreglo que formateamos con JSON.stringify
*/
$('#bookSave').click(function() {
var newBook = {
'titulo' : $('#bookTitle').val(),
'id_autor' : $('#bookAuthorId').val(),
'id_genero' : $('#bookGenreId').val()
}
$('#messages').first('p').text('Guardando libro...');
$('#messages').show();
$.ajax({
'url' : 'http://localhost:8000/books/',
'method' : 'POST',
'data' : JSON.stringify( newBook ),
'success' : function(data){
$('#messages').hide();
addBook(newBook);
}
})
});
</script>
</html>