2013-12-14 42 views
0

我想从github获得用户回购。我使用的代码不起作用我想从github获得用户回购

这里是我试图实现它的地方。 http://codeforu.ms/staff/matthew/#ourTab

它在这的jsfiddle http://jsfiddle.net/wh1t3w0lf21/nT5wY/3/

工作,这里是代码

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script> 
<script type="text/javascript"> 
    $("#btn_get_repos").click(function() { 
    $.ajax({ 
     type: "GET", 
     url: "https://api.github.com/users/google/repos", 
     dataType: "json", 
     success: function(result) { 
      for(i in result) { 
       $("#repo_list").append(
        "<li><a href='" + result[i].html_url + "' target='_blank'>" + 
        result[i].name + "</a></li>" 
       ); 
       console.log("i: " + i); 
      } 
      console.log(result); 
      $("#repo_count").append("Total Repos: " + result.length); 
     } 
    }); 
}); 
    </script> 

<li id="ourTab" class="profileContent"> 
    <div id="repos"> 
     <ul id="repo_list"></ul> 
    </div> 
    <div id="repo_content"></div> 
    <button id="btn_get_repos">Get Repos</button><span id="repo_count"></span> 
</li> 
+1

你的问题是什么? –

+0

哦。我无法让它工作。 – Matthew

+0

什么应该工作?它应该如何工作? – Sergio

回答

1

我觉得你只是订阅到事件之前的DOM已准备就绪。就像这样包装它:

$(function() { 
    $("#btn_get_repos").click(function() { 
     $.ajax({ 
      type: "GET", 
      url: "https://api.github.com/users/google/repos", 
      dataType: "json", 
      success: function(result) { 
       for (i in result) { 
        $("#repo_list").append(
        "<li><a href='" + result[i].html_url + "' target='_blank'>" + 
        result[i].name + "</a></li>" 
        ); 
        console.log("i: " + i); 
       } 
       console.log(result); 
       $("#repo_count").append("Total Repos: " + result.length); 
      } 
     }); 
    }); 
}); 
+0

它工作!谢谢!!!还有,你知道是否有可能列出存储库而不必点击那个按钮? – Matthew

+0

你刚才想要在页面加载时列出它们?只需删除事件点击订阅,并在页面准备就绪。 – crad