嘿伙计们当我通过ajax调用发送请求时,我试图发送我的个人访问令牌,但令牌没有传递给请求。继续看到API rate limit exceeded for 94.143.188.0. (But here's the good news: Authenticated requests get a higher rate limit.
错误。我试过做beforeSend:
但是没有用。我搞砸了某个地方,不知道在哪里。
由于
<div>
<button id="getGithub">Github</button>
<div id="gitResults"></div>
</div>
<div>
<button id="getCodewars">Codewars</button>
<div id="cwarsResults"></div>
</div>
var getGithub = document.getElementById('getGithub');
var getCodewars = document.getElementById('getCodewars');
var gitResults = document.getElementById('gitResults');
var cwarsResults = document.getElementById('cwarsResults');
var token = 'bea978891ecb5acbfdb7c7c5cf141dd8889b6249';
getGithub.onclick = function() {
$.getJSON({
method: "GET",
headers: { username: token },
url: "https://api.github.com/users/dantesolis",
dataType: "jsonp",
success: function( returnData ) {
console.log('This is the returnData' + returnData);
console.log(returnData.data);
for (var props in returnData.data) {
console.log(returnData.data[props]);
gitResults.innerHTML = gitResults.innerHTML + returnData.data[props]
}
//gitResults.innerHTML = returnData.data.avatar_url;
}
})
}
答案 0 :(得分:0)
好的,所以在阅读了更多帖子并搜索谷歌后,我发现如果我将我的令牌作为参数传递它就可以了。这是非常不优雅的,它只是绕过了我所拥有的问题。主要是不知道如何在ajax请求中正确设置标头,但对于那些寻找答案的人。我正在发布我的代码。如果有人有更好的解决方案,请添加它。
<div>
<button id="getGithub">Github</button>
<div id="gitResults"></div>
</div>
var getGithub = document.getElementById('getGithub');
var gitResults = document.getElementById('getGithub');
getGithub.onclick = function() {
var root = "https://api.github.com";
var user = '/users/dantesolis';
var gitHubToken = 'my_cool_token';
$.getJSON({
method: "GET",
url: root+user+"?&access_token="+gitHubToken,
dataType: "json",
success: function( response ) {
console.log('This is the returnData' + response);
var link = document.createElement('a');
var p1 = document.createElement('p');
var p2 = document.createElement('p');
link.href = response.html_url;
link.innerHTML = '@dantesolis';
p1.innerHTML = response.followers;
p2.innerHTML = response.following;
gitResults.appendChild(link);
gitResults.appendChild(p1);
gitResults.appendChild(p2);
}, /* End of success */
error: function (xhr, status, errorThrown) {
console.log('Error');
console.log(xhr.status);
}
})
}