Created
June 20, 2024 17:41
-
-
Save ggMartinez/9e884d843dc2bab205ea9ae619993209 to your computer and use it in GitHub Desktop.
Consumir API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
$("#boton").click(function(){ | |
var nombre = $("#nombre").val(); | |
var precio = $("#precio").val(); | |
var data = { | |
"nombre": nombre, | |
"precio": precio, | |
}; | |
$.ajax({ | |
url: 'http://localhost:8000/api/pizza/', | |
type: 'POST', | |
headers: { | |
"Accept" : "application/json", | |
"Content-Type" : "application/json", | |
}, | |
data: JSON.stringify(data), | |
success: function(data) { | |
alert("Se ha creado correctamente"); | |
}, | |
error: function(data){ | |
alert("No se ha podido crear"); | |
} | |
}); | |
}); | |
}); | |
</script> | |
Nombre <input type="text" id="nombre" name="nombre"> | |
Precio <input type="text" id="precio" name="precio"> | |
<button id="boton">Enviar</button> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
$.ajax({ | |
url: 'http://localhost:8000/api/pizza/', | |
type: 'get', | |
headers: { | |
"Accept" : "application/json", | |
"Content-Type" : "application/json", | |
}, | |
success: function(data) { | |
$.each(data, function(i, item) { | |
$("#pizzas").append(data[i].nombre + " - " + data[i].precio + "<br>"); | |
}); | |
}, | |
error: function(data){ | |
alert("No se ha podido crear"); | |
} | |
}); | |
}); | |
</script> | |
<h1>Pizzas</h1> | |
<div id="pizzas"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment