Github API获取带有ajax的链接头

Jon*_* M. 6 javascript jquery http-headers github-api

我使用github上的API一个小的web应用程序,并在某些时候我需要获得链接标题分页.

最终目标是获取每个存储库的提交总数,我发现python脚本并尝试使其适应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
});
Run Code Online (Sandbox Code Playgroud)

userrepo分别是用户名和他的回购名称

这是一个Github页面,所以我只能使用JavaScript.

Iva*_*zak 5

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

基本上,如果您使用JSONP来调用API,那么您将不会获得Link标题,但您将在响应JSON文档(即正文)中获得相同的信息.以下是API文档中的示例,请注意对象Link中的meta属性

$ 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
  }
})
Run Code Online (Sandbox Code Playgroud)