Javascript GithubAPI使用ajax获取链接头
我正在为一个小型web应用程序使用github api,在某个时候我需要获得 最终的目标是获得每个存储库的提交总数,我发现了这一点,并尝试将其调整为JavaScriptJavascript GithubAPI使用ajax获取链接头,javascript,jquery,http-headers,github-api,Javascript,Jquery,Http Headers,Github Api,我正在为一个小型web应用程序使用github api,在某个时候我需要获得 最终的目标是获得每个存储库的提交总数,我发现了这一点,并尝试将其调整为JavaScript getData = $.getJSON('https://api.github.com/repos/'+user+'/'+repo+'/commits?callback=?', function (commits){ console.log(getData.getResponseHeader('link'))
getData = $.getJSON('https://api.github.com/repos/'+user+'/'+repo+'/commits?callback=?', function (commits){
console.log(getData.getResponseHeader('link'))
// will return null
console.log(getData.getAllResponseHeaders('link'))
// will return an empty string
console.log(commits)
// will successfuly return my json
});
user
和repo
分别是用户名和他的repo名称
这是一个Github页面,所以我只能使用JavaScript 有关使用JSONP回调的信息,请参阅GitHub API文档:
getData = $.getJSON('https://api.github.com/repos/'+user+'/'+repo+'/commits?callback=?', function (commits){
console.log(getData.getResponseHeader('link'))
// will return null
console.log(getData.getAllResponseHeaders('link'))
// will return an empty string
console.log(commits)
// will successfuly return my json
});
基本上,如果使用JSONP调用API,则不会得到链接
头,而是会在响应JSON文档(即主体)中获得相同的信息。下面是API文档中的示例,请注意meta
对象中的Link
属性
$ curl https://api.github.com?callback=foo
foo({
"meta": {
"status": 200,
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4966",
"Link": [ // pagination headers and other links
["https://api.github.com?page=2", {"rel": "next"}]
]
},
"data": {
// the data
}
})
传递给方法的函数的签名是 类型:函数(纯对象数据、字符串textStatus、jqXHR jqXHR) 要访问链接头,应使用jqXHR对象而不是数据对象:
getData = $.getJSON(
'https://api.github.com/repos/'+user+'/'+repo+'/commits?callback=?',
function (data, textStatus, jqXHR){
console.log(jqXHR.getResponseHeader('Link'))
// will return the Header Link
console.log(commits)
// will successfuly return my json
});