gpt4 book ai didi

javascript - Github API 使用 ajax 获取链接头

转载 作者:数据小太阳 更新时间:2023-10-29 05:23:27 24 4
gpt4 key购买 nike

我正在为一个小型网络应用程序使用 github api,有时我需要获取 link header对于the pagination .

最终目标是获取每个存储库的提交总数,我发现 python script并尝试使其适应 JavaScript。

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
});

userrepo分别是用户名和他的repo名

它用于 Github 页面,所以我只能使用 JavaScript。

最佳答案

有关使用 JSONP 回调的信息,请参阅 GitHub API 文档:http://developer.github.com/v3/#json-p-callbacks

基本上,如果您使用 JSONP 调用 API,那么您将不会获得 Link header ,但您会在响应 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
}
})

关于javascript - Github API 使用 ajax 获取链接头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14169308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com