2014-04-23 62 views

回答

4

這裏是一些基本的代碼來從github回購所有問題。除jQuery外不使用任何庫。

function getIssues(opts){ 
    opts.data = opts.data || []; 
    opts.page = opts.page || 1; 
    var url = 'https://api.github.com/repos/' + opts.username + '/' + opts.repo; 
    url += '/issues?callback=?&page=' + opts.page + '&per_page=100&state=' + opts.state; 
    $.ajax(url, { 
     dataType: 'jsonp', 
     success: function(res){ 
      if(res.meta && res.meta.status == '403'){ 
       return opts.error(res.data); 
      } 
      opts.data = $.merge(opts.data, res.data); 
      if(res.meta && res.meta.Link){ 
       if(res.meta.Link[0][1].rel == "next"){ 
        opts.page++; 
        getIssues(opts) 
       } else { 
        opts.success(opts.data); 
       } 
      } 
     } 
    }); 
} 

用法示例:

getIssues({ 
    username: 'joyent', 
    repo: 'node', 
    state: 'open', 
    success: function(data){ 
     console.log(data); 
    }, 
    error: function(err){ 
     console.log(err); 
    } 
}); 

注意:如果您的請求是沒有認證的,你將被限制在每小時60個請求(每個IP地址)。 See here

+0

這就是它,完美的作品,非常感謝! –

相關問題