GitHub API 用户
GitHub API Users
我有一个关于 GitHub API 的问题。
我才刚刚开始学习它。对不起我的错误。
我需要使用 GitHub API 获取用户列表。但我不知道该怎么做:(
非常感谢您的帮助!
这就是我们现在拥有的:
<!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>
除非您的 html 托管在 api.github.com
上的某处,或者您采取了其他特定步骤,否则这将不起作用。
继续阅读 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.
出于安全原因,浏览器将不允许 javascript 向其他域发出请求,除非明确允许。
另请参阅:
- JSON Get request using JQuery (cross-domain)
- Ways to circumvent the same-origin policy
- Example of using github API from javascript
我有一个关于 GitHub API 的问题。 我才刚刚开始学习它。对不起我的错误。
我需要使用 GitHub API 获取用户列表。但我不知道该怎么做:(
非常感谢您的帮助!
这就是我们现在拥有的:
<!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>
除非您的 html 托管在 api.github.com
上的某处,或者您采取了其他特定步骤,否则这将不起作用。
继续阅读 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.
出于安全原因,浏览器将不允许 javascript 向其他域发出请求,除非明确允许。
另请参阅:
- JSON Get request using JQuery (cross-domain)
- Ways to circumvent the same-origin policy
- Example of using github API from javascript