Skip to content

Commit

Permalink
session 28: fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeeppy committed Apr 26, 2023
1 parent 73d4a10 commit 99053ed
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
30 changes: 30 additions & 0 deletions session28/fetch-get/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!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>Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"/>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div class="container">

<div class="row justify-content-center">

<div class="col-md-8">
<h1 class="text-center mt-5">Bitcoin Price</h1>

<div id="price" class="text-center">
<span id="price_label">xxxx.xx</span>
</div>
</div>

</div>

</div>

<script src="script.js"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions session28/fetch-get/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const url = 'https://blockchain.info/ticker';

async function recupererPrix() {
const requete = await fetch(url, {
method: 'GET'
});

if (!requete.ok) {
alert('Un problème est survenu');
} else {
let donnees = await requete.json();
document.querySelector("#price_label").textContent = donnees.EUR.last;
}
}

setInterval(recupererPrix, 1000);
7 changes: 7 additions & 0 deletions session28/fetch-get/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
h1 {
color: #272343;
}

#price {
font-size: 4em;
}
47 changes: 47 additions & 0 deletions session28/fetch-post/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const url = 'https://lesoublisdelinfo.com/api.php';

async function envoyerPrenom(prenom) {
const requete = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
prenom
})
});

if (!requete.ok) {
alert('Un problème est survenu');
} else {
const donnees = await requete.json();
console.log(donnees);
}
}

envoyerPrenom('Teddy');

// let requete = new XMLHttpRequest();

// // Get
// // requete.open('GET', url);
// // requete.responseType = 'json';
// // requete.send();

// // Post
// requete.open('POST', url);
// requete.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// requete.responseType = 'json';
// requete.send('prenom=John');

// requete.onload = function() {
// if (requete.readyState === XMLHttpRequest.DONE) {
// if (requete.status === 200) {
// let reponse = requete.response;
// console.log(reponse);
// }
// else {
// alert('Un problème est intervenu, merci de revenir plus tard.');
// }
// }
// }

0 comments on commit 99053ed

Please sign in to comment.