feat(js): genders example

This commit is contained in:
Rafa Muñoz
2025-03-24 14:48:58 +01:00
parent a1fd008a8e
commit a2283cbce2

View File

@@ -11,29 +11,68 @@
const xhttp = new XMLHttpRequest(); const xhttp = new XMLHttpRequest();
xhttp.onload = function () { xhttp.onload = function () {
let strResponse = xhttp.responseText;
console.log("Datos recibidos: " + strResponse); if (xhttp.readyState === 4) { // 4 = La solicitud ha sido completada
let jsonResponse = JSON.parse(strResponse); if (xhttp.status === 200 || xhttp.status === 201) {
console.log("Datos parseados: " + jsonResponse); let strResponse = xhttp.responseText;
for (let i = 0; i < jsonResponse.length; i++) { console.log("Datos recibidos: " + strResponse);
let genero = jsonResponse[i]; let jsonResponse = JSON.parse(strResponse);
console.log("Género: " + genero.id + " - " let responseArray = jsonResponse.content;
+ genero.nombre); console.log("Datos parseados: " + responseArray);
gendersTable.innerHTML += '<tr>' + for (let i = 0; i < responseArray.length; i++) {
'<td>' + genero.id + '</td>' + let genero = responseArray[i];
'<td>' + genero.nombre + '</td>' + console.log("Género: " + genero.id + " - "
'</tr>' + genero.nombre);
gendersTable.innerHTML += '<tr>' +
'<td>' + genero.id + '</td>' +
'<td>' + genero.nombre + '</td>' +
'</tr>'
}
}
else {
console.log('Error al obtener la petición xhr.status: ' + xhttp.status);
}
} }
} }
// Send a request // Send a request
xhttp.open("GET", "http://localhost:8081/v1/genders"); xhttp.open("GET", "http://localhost:8081/v1/genders?page=0&size=100&fieldOrder=id&orderType=desc");
xhttp.send(); xhttp.send();
} }
function updateGender() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function () {
if (xhttp.readyState === 4) { // 4 = La solicitud ha sido completada
if (xhttp.status === 200 || xhttp.status === 201) {
alert("Modificado correctamente");
}
else {
console.log('Error al obtener la petición xhr.status: ' + xhttp.status);
}
}
}
// Send a request
xhttp.open("PUT", "http://localhost:8081/v1/genders/1");
xhttp.setRequestHeader("accept","application/json");
xhttp.setRequestHeader("Content-Type","application/json");
xhttp.send(JSON.stringify({
"id": 1,
"nombre": "Ciencia Ficción modificado 2"
}));
}
</script> </script>
</head> </head>
<body onload="loadGenders()"> <body onload="loadGenders()">
<button onclick="updateGender();">Modificar género</button>
<table id="gendersTable" border="1" width="100%"> <table id="gendersTable" border="1" width="100%">
<tr> <tr>
<th>Id</th> <th>Id</th>