2013-01-05 2 views
6

작은 웹 앱에 github api를 사용하고 어느 시점에 the paginationlink header을 가져와야합니다.Github API는 아약스로 링크 헤더를 얻습니다.

최종 목표는 리포지토리 당 커밋의 총 수를 얻는 것입니다. 나는 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에서의 페이지입니다

있습니다. 당신이 API를 호출하는 JSONP를 사용하는 경우 http://developer.github.com/v3/#json-p-callbacks

기본적으로, 당신은 Link 헤더를받지 않습니다,하지만 당신은 대신에 동일한 정보를 얻을 것이다 :

답변

5

은 JSONP 콜백을 사용하기위한 GitHub의의 API 문서를 참조하십시오 응답 JSON 문서 (즉 본문)에. 기능 (PlainObject 데이터, 문자열 textStatus, jqXHR jqXHR) : 다음은 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 
    } 
}) 
+0

완벽, 감사 –

0

당신이 getJSON 방법에 전달 함수에 대한 서명은 유형입니다

가 대신 데이터 객체의 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 
    });