1

I have a question about GitHub API. I'm just starting to learn it. Sorry for my mistakes.

I need to get a list of users using GitHub API. But I cannot figure out how to do it :(

I would be extremely grateful to you for your help!

That is what we have now:

<!doctype html>
<html lang="en-US">
<head>
  <meta charset="utf-8">
  <meta http-equiv="Content-Type" content="text/html">
  <title>GITHUB - API Test</title>
  <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>

<body>
    <div id="ghapidata"></div>
<script type="text/javascript">
$(function() {
 $('#ghapidata').html('Loading...');
 var username = $('#ghusername').val();
 var requri = 'https://api.github.com/users/' + "Test";
 var repouri = 'https://api.github.com/users/' + "Test" + '/repos';
 requestJSON(requri, function(json) {
  var fullname = json.name;
  var username = json.login;
  var aviurl = json.avatar_url;
  var profileurl = json.html_url;
  var location = json.location;
  var followersnum = json.followers;
  var followingnum = json.following;
  var reposnum = json.public_repos;
  var email = json.email;
  if (fullname == undefined) {
   fullname = username;
  }
  var outhtml = '<h2>'+ 'Username:' + fullname + ' <span class="smallname">(@<a href="' + profileurl + '" target="_blank">' + username + '</a>)</span></h2>';
  outhtml = outhtml + '<a href="' + profileurl + '" target="_blank"><img src="' + aviurl + '" width="80" height="80" alt="' + username + '"></a>';
  outhtml = outhtml + '<p>Subscribers: ' + followersnum + ' - Subscribe: ' + followingnum +'</p>';
  outhtml = outhtml + '<div class="clearfix">';
  var repositories;
  $.getJSON(repouri, function(json) {
   repositories = json;
   outputPageContent();
  });

  function outputPageContent() {
    outhtml = outhtml + '</ul></div>';
   $('#ghapidata').html(outhtml);
  }
 });

 function requestJSON(url, callback) {
  $.ajax({
   url: url,
   complete: function(xhr) {
    callback.call(null, xhr.responseJSON);
   }
  });
 }
});
</script>
</body>
</html>
ProstoJohn
  • 13
  • 4
  • Hi! Welcome to Stack Overflow. Please add some information about what results or errors you get and what results you want. This will help people understand what the actual problem is. – bernie Sep 17 '15 at 17:12
  • Hello, no problem. My task: Display a list of users from here: https://api.github.com/users , in an understandable form (Names, Avatars). At the moment, I was able to bring just one user. – ProstoJohn Sep 17 '15 at 17:31

1 Answers1

0

Unless your html is hosted somewhere on api.github.com, or you take other specific steps, this won't work.

Read up on Same-Origin policy:

In computing, the same-origin policy is an important concept in the web application security model. Under the policy, a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin. An origin is defined as a combination of URI scheme, hostname, and port number.1 This policy prevents a malicious script on one page from obtaining access to sensitive data on another web page through that page's Document Object Model.

For security reasons, a browser will not allow javascript to make requests to other domains unless explicitly allowed.

See also:

Community
  • 1
  • 1
bernie
  • 9,820
  • 5
  • 62
  • 92
  • No, I have my own server. That is to take the information from this http://api.github.com/users And provide for people in an understandable form. It is impossible to implement? – ProstoJohn Sep 17 '15 at 18:10
  • One possible solution is to proxy request to the github api via your server. Have you read links I gave you? – bernie Sep 17 '15 at 18:12
  • Let's say your page is located at `www.myserver.com/mypage`. You are only allowed to make ajax requests to other urls on `www.myserver.com`. This is to prevent for instance making requests to `www.mybank.com` and pretending to be the bank's website. – bernie Sep 17 '15 at 18:19
  • With AJAX, I can realize the task? – ProstoJohn Sep 17 '15 at 18:22
  • You are already using ajax in your code snippet. I'm sorry but it looks like you need to read up more on ajax before you can accomplish this task. Your questions are simply too broad to answer here. – bernie Sep 17 '15 at 18:27
  • Well I'll try to find out! Thanks for the info – ProstoJohn Sep 17 '15 at 18:29