Created
November 12, 2022 15:38
-
-
Save koraytugay/9356faf9371654dd5566bd3b2e1f4a3c to your computer and use it in GitHub Desktop.
Fetch vs XMLHttpRequest
This file contains 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, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>On Load</title> | |
</head> | |
<body> | |
<ul id="list"></ul> | |
<script> | |
const list = document.getElementById('list'); | |
fetch('https://mockyard.herokuapp.com/users') | |
.then(response => response.json()) | |
.then(personArray => { | |
personArray.forEach(person => { | |
const listItem = document.createElement('li'); | |
listItem.textContent = person.name; | |
list.appendChild(listItem); | |
}); | |
}); | |
// const httpRequest = new XMLHttpRequest(); | |
// httpRequest.onload = () => { | |
// JSON.parse(httpRequest.responseText).forEach(person => { | |
// const listItem = document.createElement('li'); | |
// listItem.textContent = person.name; | |
// list.appendChild(listItem); | |
// }); | |
// }; | |
// | |
// httpRequest.open('GET', 'https://mockyard.herokuapp.com/users', true); | |
// httpRequest.send(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment