2014-02-16 42 views
0

我想有一个用户点击一个按钮来检查通过API的东西的状态。我究竟做错了什么?按钮来调用Javascript函数和解析JSON API结果

HTML:

<button onclick="myFunction('http://example.com/api')">Try it</button> 

JS:

<script> 
    function myFunction(url) { 
     $.get(url, function (jQuery.parseJSON(result)) { 
      alert(result.status); 
     }); 
    } 
</script> 

回答

0

继为我工作。我测试了我的自我。有关详细信息,请参阅http://api.jquery.com/jQuery.get/

我使用的测试网址类似以下内容:

<button onclick="myFunction('https://api.github.com/users/mralexgray/repos')">Try it</button> 

你的函数应该像下面:

function myFunction(url) { 

     $.get(url, function (data) { 
      var temp = data[0] 
      alert("Data Loaded: " + data); 
      alert("temp.id = " + temp.id); 
      alert("temp.name = " + temp.name); 
     }); 
    }