我有一个关于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>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>最满意答案
除非您的html托管在api.github.com某个地方,否则您采取其他具体步骤,这将无效。
阅读Same-Origin政策 :
在计算中,同源策略是Web应用程序安全模型中的一个重要概念。 根据该策略,Web浏览器允许第一个Web页面中包含的脚本访问第二个Web页面中的数据,但前提是两个Web页面具有相同的源。 原点定义为URI方案,主机名和端口号的组合。 1此策略可防止一个页面上的恶意脚本通过该页面的文档对象模型访问另一个网页上的敏感数据。
出于安全原因,除非明确允许,否则浏览器不允许javascript向其他域发出请求。
也可以看看:
JSON使用JQuery获取请求(跨域) 如何规避同源政策 从javascript使用github API的示例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:
JSON Get request using JQuery (cross-domain) Ways to circumvent the same-origin policy Example of using github API from javascript
更多推荐
发布评论