我正在尝试制作一个小部件,根据会员ID显示某人属于的Meetup小组。
这是我的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时显示响应。
答案 0 :(得分:2)
Herp Derp。 API请求网址中需要 ?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>
答案 1 :(得分: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);});
这可以避免不必要的功能定义,并使您的代码更清晰。