jsonp 循环数据

发布于 2024-10-05 01:45:13 字数 494 浏览 0 评论 0原文

我有一个返回 jsonp 格式的网络服务。这是代码:

$(document).ready(function(){               
       $.getJSON("http://api.tubeupdates.com/?method=get.status&lines=central,victoria&return=name&jsonp=?",
             function (result){
                  $.each(result.items, function(item){
                     $('body').append(item.response.lines[0].name);

             });
        }
     );

});

如果我删除循环,它可以正常工作,但 $.each 循环会失败。 知道我做错了什么吗?

谢谢

毛罗

I have a webservice returning jsonp format. here's the code:

$(document).ready(function(){               
       $.getJSON("http://api.tubeupdates.com/?method=get.status&lines=central,victoria&return=name&jsonp=?",
             function (result){
                  $.each(result.items, function(item){
                     $('body').append(item.response.lines[0].name);

             });
        }
     );

});

It works fine if I remove the loop but fails with the $.each loop.
Any idea of what am I doing wrong?

Thanks

Mauro

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2024-10-12 01:45:13

响应的 items 属性上没有数组,它看起来像这样:

{"response":{"lines":[{"name":"Central"},{"name":"Victoria"}]}}

看起来您想要迭代 response.lines 数组,在这种情况下您需要为此:

$.each(result.response.lines, function(i, item){
   $('body').append(item.name);
});

您可以在此处进行测试

The response doesn't have an array on an items property, it looks like this:

{"response":{"lines":[{"name":"Central"},{"name":"Victoria"}]}}

It looks like you want to iterate the response.lines array, in which case you need to do this:

$.each(result.response.lines, function(i, item){
   $('body').append(item.name);
});

You can test it out here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文