I am trying to get some data from the GitHub API using jQuery(AJAX), and appending it in a static webpage. Below are my HTML and JS snippets.
$(document).ready(function(){
$.ajax({
url: 'https://api.github.com/repos/FearlessPython/Basics-of-Python',
}).done(function(repo){
$('#heading').html(`${repo.name}<hr>`);
$('#stats').html(`
<strong>Stars</strong>: ${repo.stargazers_count} <br>
<strong>Watchers</strong>: ${repo.watchers_count} <br>
<strong>Language</strong>: ${repo.language} <br>
<strong>Open Issues</strong>: ${repo.open_issues_count} <br>
<strong>License</strong>: ${repo.license.name}<br>
`);
});
$.ajax({
method: "GET",
url: "https://api.github.com/repos/FearlessPython/Basics-of-Python/contributors",
dataType: "json"
}).done(function(data){
$.each(data, function(index, url){
$.ajax({
url: data[index].url
}).done(function(individual_contributor){
// console.log(data[index].url);
$('ul#users').append(`
<li><strong>${individual_contributor.name}</strong></li>
<li><strong>Bio:</strong> ${individual_contributor.bio}</li>
<li><strong>Public Repos:</strong> ${individual_contributor.public_repos}</li>
<li><strong>Followers:</strong> ${individual_contributor.followers}</li>
<li><strong>Following:</strong> ${individual_contributor.following}</li>
`)
});
});
$.map(data, function(contributor, i){
// contrb.push(contributor);
$('ol#contributors').append(`
<li><a href="${contributor.html_url}" target="_blank">${contributor.login}</a></li>
<img src = "${contributor.avatar_url}" style = "float: right;" width=10%>
<ul id="users"></ul>
`);
});
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
</head>
<body>Here, dynamic statistics are available regarding this repository in GitHub.
<div id="statistics">
<h3 id="heading"></h3>
<ul id="stats"></ul>
<h4>Contributors</h4><hr>
<ol id="contributors">
<!-- <ul id="users"></ul> -->
</ol>
</div>
</body>
</html>
You can see that under each user, the same thing iterates again and again. Using .html()
instead of .append()
overwrites the first iteration data and everything gets replaced by the latest iteration's data. The ideal result should be like this:
I also tried to use for
loop, but the same thing is happening. I am trying to solve it from the last few days, but unable to solve it. I have just started using jQuery and AJAX.
Note: In case if the GitHub API requests exceed for you, then you won't be able to see the result of the above code snippets. So I am also including an image of the current situation of the Webpage.