| Server IP : 109.234.162.214 / Your IP : 216.73.216.222 Web Server : Apache System : Linux servd162214.srv.odns.fr 4.18.0-372.26.1.lve.1.el8.x86_64 #1 SMP Fri Sep 16 14:08:19 EDT 2022 x86_64 User : carpe ( 1178) PHP Version : 8.0.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/carpe/public_html/ajax/ |
Upload File : |
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="main2.css">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<div id='contain'>
</div>
<script>
//afficher des données venant d'une source exterieur en utilisant une API et en triant les données en JSON.
$.ajax({
url: "https://fakestoreapi.com/products/",
dataType: 'json',
success: function(res) {
const products = res.map(product => {
return {
id: product.id,
name: product.title,
price: product.price
};
});
console.log(products)
//console.log(res) //reponse sous forme de tableau d'objets
res.forEach((obj, index) => {
//trier le JSON
//console.log(obj['id'])
//console.log(obj['title'])
$('#contain').append('Id : ' + obj['id'] + " Title : " + obj['title'] + ' Prix : ' + obj['price'] + "</br>");
});
},
error: function(err) {
console.error("Erreur lors de la requête AJAX : ", err);
}
});
</script>
</body>
</html>