Skip to content

Instantly share code, notes, and snippets.

@koraytugay
Created November 12, 2022 15:38
Show Gist options
  • Save koraytugay/9356faf9371654dd5566bd3b2e1f4a3c to your computer and use it in GitHub Desktop.
Save koraytugay/9356faf9371654dd5566bd3b2e1f4a3c to your computer and use it in GitHub Desktop.
Fetch vs XMLHttpRequest
<!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