Skip to content

Instantly share code, notes, and snippets.

@paragchirde
Last active June 19, 2020 09:43
Show Gist options
  • Save paragchirde/a87ec09d1854a1839491ff3fa4d1bc16 to your computer and use it in GitHub Desktop.
Save paragchirde/a87ec09d1854a1839491ff3fa4d1bc16 to your computer and use it in GitHub Desktop.
Fetching posts from an API and displaying them in the form of Bootstrap cards.
<!doctype html>
<html lang="en">
<head>
<title>Foobler</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<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>
<div class="container mt-4" id="postContainer">
<!-- All posts goes here -->
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
// https://jsonplaceholder.typicode.com/photos
// https://jsonplaceholder.typicode.com/users
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response =>
response.json()
)
.then(posts => {
console.log(posts)
posts.forEach((post) => {
var template = '<div class="card mt-2">'+
'<div class="card-body">'+
'<p class="card-title">Post ID: '+post.id+'</p>'+
'<h5 class="card-title">'+post.title+'</h5>'+
'<p class="card-text">'+ post.body +'</p>'+
'<a href="#" class="btn btn-primary">Go somewhere</a>'+
' </div>'+
'</div>';
document.getElementById('postContainer').innerHTML += template;
})
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment