En esta lección se explica el funcionamiento de un servicio muy sencillo.
Puedes probar el ejemplo en https://replit.com/@GilbertoPachec5/srvejemplo?v=1. Hazle fork al proyecto y córrelo.
const resp =
await fetch("servicio.php")
if (resp.ok) {
const texto =
await resp.text()
alert(texto)
} else {
throw new Error(
resp.statusText)
}
const resp =
await fetch("servicio.php")
if (resp.ok) {
const texto =
await resp.text()
alert(texto)
} else {
throw new Error(
resp.statusText)
}
Ejecuta fetch y envía request (solicitud).
servicio.php
GET
<?php
echo "Hola";
Despierta y recibe request.
const resp =
await fetch("servicio.php")
if (resp.ok) {
const texto =
await resp.text()
alert(texto)
} else {
throw new Error(
resp.statusText)
}
Hace wait esperando response.
<?php
echo "Hola";
Procesa la request y genera
la response (respuesta).
El code (o código) 200
indica que terminó
con éxito.
const resp =
await fetch("servicio.php")
if (resp.ok) {
const texto =
await resp.text()
alert(texto)
} else {
throw new Error(
resp.statusText)
}
Despierta y recibe response.
resp tiene lo mismo
que la response,
pero en un objeto
de JavaScript.
<?php
echo "Hola";
Devuelve response y se duerme.
const resp =
await fetch("servicio.php")
if (resp.ok) {
const texto =
await resp.text()
alert(texto)
} else {
throw new Error(
resp.statusText)
}
const resp =
await fetch("servicio.php")
if (resp.ok) {
const texto =
await resp.text()
alert(texto)
} else {
throw new Error(
resp.statusText)
}
El texto recuperado
queda en la
constante texto.
const resp =
await fetch("servicio.php")
if (resp.ok) {
const texto =
await resp.text()
alert(texto)
} else {
throw new Error(
resp.statusText)
}
Hola
const resp =
await fetch("servicio.php")
if (resp.ok) {
const texto =
await resp.text()
alert(texto)
} else {
throw new Error(
resp.statusText)
}
Revisa el proyecto en Replit con la URL https://replit.com/@GilbertoPachec5/srvejemplo?v=1. Hazle fork al proyecto y córrelo. En el ambiente de desarrollo tienes la opción de descargar el proyecto en un zip.
Crea un proyecto PHP Web Server en Replit y edita o sube los archivos de este proyecto.
Observa paso a paso la comunicación entre ciente y servidor.
1 | <!DOCTYPE html> |
2 | <html lang="es"> |
3 | |
4 | <head> |
5 | |
6 | <meta charset="UTF-8"> |
7 | <meta name="viewport" content="width=device-width"> |
8 | |
9 | <title>Ejemplo de servicio</title> |
10 | |
11 | </head> |
12 | |
13 | <body> |
14 | <h1>Ejemplo de servicio</h1> |
15 | |
16 | <button onclick="ejecuta()"> |
17 | Ejecuta |
18 | </button> |
19 | |
20 | <script> |
21 | |
22 | async function ejecuta() { |
23 | try { |
24 | // Se conecta a servicio.php y recibe su respuesta. |
25 | const resp = |
26 | await fetch("servicio.php") |
27 | if (resp.ok) { |
28 | const texto = |
29 | await resp.text() |
30 | alert(texto) |
31 | } else { |
32 | throw new Error( |
33 | resp.statusText) |
34 | } |
35 | } catch (error) { |
36 | console.error(error) |
37 | alert(error.message) |
38 | } |
39 | } |
40 | |
41 | </script> |
42 | |
43 | </body> |
44 | |
45 | </html> |
1 | <?php |
2 | |
3 | echo "Hola"; |
4 |
En esta lección se mostró el funcionamiento de un servicio.