0

我正在尝试制作一个小部件,该小部件将根据其成员 ID 显示某人所属的聚会组。

这是我的 HTML 文件。

<!DOCTYPE html>
<html>
<head>
<title>Meetup Groups by Member ID</title>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script>
$(document).ready(function(){
$.getJSON("https://api.meetup.com/2/groups?&sign=true&member_id=8377069&page=20&api&key=API_KEY&only=name,link", displayGroups);

function displayGroups(data) {
    var htmlString = "";
    $.each(data.items, function (i, item) {
        htmlString += '<h3><a href="' + item.link + '" target="_blank">' + item.name + '</a></h3>';
    });
    $('#groups').html(htmlString);
}
});
</script>
</head>

<body>
<div id="groups"></div>
</body>

</html>

这是 JSON 输出(当我将请求 URL 放入浏览器时):

{"results":[{"name":"The New York Python Meetup Group","link":"http:\/\/www.meetup.com\/nycpython\/"},{"name":"WordPress NYC Meetup Group","link":"http:\/\/www.meetup.com\/WordPressNYC\/"},{"name":"Coworking Community NYC","link":"http:\/\/www.meetup.com\/coworking-nyc\/"},{"name":"NYC Ruby Roundtable","link":"http:\/\/www.meetup.com\/nycruby\/"},{"name":"Why Leave Astoria?!","link":"http:\/\/www.meetup.com\/whyleaveastoria\/"},{"name":"#betaNYC, formerly Open NY Forum","link":"http:\/\/www.meetup.com\/betanyc\/"},{"name":"New York Open Statistical Programming Meetup","link":"http:\/\/www.meetup.com\/nyhackr\/"},{"name":"Django-NYC","link":"http:\/\/www.meetup.com\/django-nyc\/"},{"name":"NYC Redditors","link":"http:\/\/www.meetup.com\/NYC-Redditors\/"},{"name":"Startup Addicts NYC","link":"http:\/\/www.meetup.com\/startupddict\/"},{"name":"Woodside - Sunnyside Runners","link":"http:\/\/www.meetup.com\/Woodside-Sunnyside-Runners\/"},{"name":"NY HTML5 User Group","link":"http:\/\/www.meetup.com\/NY-HTML5-User-Group\/"},{"name":"Hacks\/Hackers New York","link":"http:\/\/www.meetup.com\/hackshackersny\/"},{"name":"New York City Geek Adventure Group","link":"http:\/\/www.meetup.com\/Astoria-Long-Island-City-Geek-Adventure-Group\/"},{"name":"Mukti's Kitchen","link":"http:\/\/www.meetup.com\/MuktisKitchen\/"},{"name":"Unearth Ruby","link":"http:\/\/www.meetup.com\/unearthruby\/"},{"name":"Awesome People of Sunnyside Queens Unite!","link":"http:\/\/www.meetup.com\/Awesome-People-of-Sunnyside-Queens-Unite\/"},{"name":"New York Data Visualization and Infographics","link":"http:\/\/www.meetup.com\/New-York-Datavisualization-and-Infographics\/"},{"name":"Google NYC Tech Talks","link":"http:\/\/www.meetup.com\/google-nyc-tech-talks\/"},{"name":"Crisismappers NYC","link":"http:\/\/www.meetup.com\/Crisismappers-NYC\/"}],"meta":{"lon":"","count":20,"signed_url":"http:\/\/api.meetup.com\/2\/groups?radius=25.0&order=id&desc=false&member_id=8377069&offset=0&format=json&only=name%2Clink&page=20&fields=&sig_id=8377069&sig=651d9c728df7bb62915fdc1fbae1b2b87d36c35b","link":"https:\/\/api.meetup.com\/2\/groups","next":"https:\/\/api.meetup.com\/2\/groups?key=4d366a414432e7e442d6eb1e124f&radius=25.0&order=id&member_id=8377069&format=json&page=20&desc=false&offset=1&only=name%2Clink&fields=&sign=true","total_count":26,"url":"https:\/\/api.meetup.com\/2\/groups?key=4d366a414432e7e442d6eb1e124f&radius=25.0&order=id&member_id=8377069&format=json&page=20&desc=false&offset=0&only=name%2Clink&fields=&sign=true","id":"","title":"Meetup Groups v2","updated":1366809331000,"description":"\"\"","method":"Groups","lat":""}}

我在控制台中没有收到任何错误。页面上没有打印任何内容。任何想法我做错了什么?

编辑:我想不知何故我实际上并没有检索任何数据。当我测试 HTML 页面时,控制台没有显示响应,但当我在浏览器中运行 API 调用 URL 时显示响应。

4

2 回答 2

2

赫普·德普。API 请求 URL 中需要?callback?

完整答案:

<!DOCTYPE html>
<html>
<head>
    <title>Meetup Groups by Member ID</title>
    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $.getJSON("https://api.meetup.com/2/groups?callback=?&sign=true&member_id=8377069&page=20&api&key=API_KEY&only=name,link", 
function (data) {
    var htmlString = "";
    $.each(data.results, function (i, item) {
        htmlString += '<h3><a href="' + item.link + '" target="_blank">' + item.name + '</a></h3>';
    });
    $('#groups').html(htmlString);});
    });
    </script>
    </head>

    <body>
    <div id="groups"></div>
    </body>

    </html>
于 2013-04-24T14:44:55.207 回答
1

我确实认为该函数需要在用作回调之前定义。我会将其更改为:

$.getJSON("https://api.meetup.com/2/groups?&sign=true&member_id=8377069&page=20&api&key=API_KEY&only=name,link", 
function (data) {
    var htmlString = "";
    $.each(data.items, function (i, item) {
        htmlString += '<h3><a href="' + item.link + '" target="_blank">' + item.name + '</a></h3>';
    });
    $('#groups').html(htmlString);});

这避免了不必要的函数定义并使您的代码更清晰。

于 2013-04-24T14:13:43.270 回答