Javascript 如何从GithubAPI获取所有信息
我有一个问题。我在公共GitHub API上使用vanilla JavaScript发出GET请求,得到的是响应,但不是单个用户的完整信息。我如何获得所有信息,如Twitter用户名或Twitter帐户链接 我对每个用户的唯一回应是: 以下是我的代码,以防万一:Javascript 如何从GithubAPI获取所有信息,javascript,github-api,Javascript,Github Api,我有一个问题。我在公共GitHub API上使用vanilla JavaScript发出GET请求,得到的是响应,但不是单个用户的完整信息。我如何获得所有信息,如Twitter用户名或Twitter帐户链接 我对每个用户的唯一回应是: 以下是我的代码,以防万一: function loadUsers() { const xhr = new XMLHttpRequest(); const URL = 'https://api.github.com/users'; xhr.open(
function loadUsers() {
const xhr = new XMLHttpRequest();
const URL = 'https://api.github.com/users';
xhr.open('GET', URL, true);
xhr.onload = function() {
if(this.status == 200) {
let users = this.responseText;
console.log(users);
}
}
xhr.send();
}
对于每个用户,您都会收到一个url以了解api的更多详细信息。在您的示例中,第一个用户
{
...
“url”:”https://api.github.com/users/mojombo",
...
},
或者只需遵循模式https://api.github.com/users/{username}
如果调用此url(),您将获得twitter帐户以及更多
{
“登录”:“mojombo”,
“id”:1,
“节点id”:“MDQ6VXNlcjE=”,
“头像url”:https://avatars.githubusercontent.com/u/1?v=4",
“gravatar_id”:“,
“url”:”https://api.github.com/users/mojombo",
“html_url”:”https://github.com/mojombo",
“关注者url”:https://api.github.com/users/mojombo/followers",
“以下url:”https://api.github.com/users/mojombo/following{/other_user}“,
“gists_url”:https://api.github.com/users/mojombo/gists{/gist_id}“,
“带星号的url”:https://api.github.com/users/mojombo/starred{/owner}{/repo}“,
“订阅\u url”:https://api.github.com/users/mojombo/subscriptions",
“组织\u url”:https://api.github.com/users/mojombo/orgs",
“repos_url”:https://api.github.com/users/mojombo/repos",
“事件\u url”:https://api.github.com/users/mojombo/events{/隐私}“,
“收到的\u事件\u url”:https://api.github.com/users/mojombo/received_events",
“类型”:“用户”,
“站点管理”:false,
“姓名”:“汤姆·普雷斯顿·沃纳”,
“公司”:“@chatterbugapp、@redwoodjs、@preston-werner ventures”,
“博客”:http://tom.preston-werner.com",
“地点”:“旧金山”,
“电子邮件”:空,
“可雇佣”:空,
“bio”:空,
“推特用户名”:“mojombo”,
“公共回购”:62,
“公众学者”:62,
“追随者”:22447,
“以下”:11,
“创建时间”:“2007-10-20T05:24:19Z”,
更新地址:“2021-03-18T22:50:53Z”
}
。看起来GH建议使用octokit
library.BTW,我建议使用更复杂的XMLHttpRequest
。如果您需要支持IE-use是的,我知道这是一种老方法,但现在我更喜欢使用XMLHttpRequest。无论如何谢谢你!