Skip to content

Instantly share code, notes, and snippets.

@heolin
Created November 25, 2019 15:48
Show Gist options
  • Save heolin/2f57022ce0baaaeebefb24a287ba93dc to your computer and use it in GitHub Desktop.
Save heolin/2f57022ce0baaaeebefb24a287ba93dc to your computer and use it in GitHub Desktop.
Simple axios example generating html table
<html>
<head>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<table class="table">
<thead>
<th>id</th>
<th>email</th>
<th>first name</th>
<th>last name</th>
</thead>
<tbody id="tablebodylol">
</tbody>
</table>
<script>
let userId = 1;
let url = 'https://reqres.in/api/users?page=2'
console.log("loading");
axios.get(url)
.then(function (response) {
let json = response.data;
let tableText = "";
json.data.forEach((user) => {
tableText += "<tr>";
tableText += "<td>"+user.id+"</td>";
tableText += "<td>"+user.email+"</td>";
tableText += "<td>"+user.first_name+"</td>";
tableText += "<td>"+user.last_name+"</td>";
tableText += "</tr>";
console.log(user);
});
document.getElementById('tablebodylol').innerHTML = tableText;
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
// always executed
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment