创建/访问使用jQuery $就与Last.FM API JSON对象 [英] Creating/Accessing a JSON object using jQuery $.ajax with Last.FM API

查看:103
本文介绍了创建/访问使用jQuery $就与Last.FM API JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近改变了我的网站设计和现在需要使用动态AJAX请求我的数据。基本上,我试图用Last.FM API JSON格式检索用户数据。

I've recently changed my site design and now need to use dynamic AJAX requests for my data. Basically, I'm trying to retrieve user data using the Last.FM API in JSON format.

我是新望到这一点,特别是JSON和它给了我一个有点头疼!我知道我必须失去了一些东西简单。

I'm newish to this, particularly JSON, and it's giving me a bit of a headache! I know I must be missing something simple.

下面是一些很基本的code测试的功能,但它不能获取任何东西!

Here is some very basic code to test the functionality but it's not retrieving anything!

<html>
<head>
    <script src="./jquery/jquery-1.4.4.js"></script>  
</head>
<body>
<script type="text/javascript">

$(document).ready(function() {  
    $.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json&callback=?", function(data) {
        $.each(data.topartists.artist, function(i,item){
            html += "<p>" + item.name + " - " + item.playcount + "</p>";    
        });
        $('#test').append(html);
    });
});
</script>

<div id="test"></div>

</body></html>

有什么建议?

我希望能够使用JSON对象在整个页面的话,例如,在任何时候,我可以叫topartists.artist [I] .playcount;显示playcount,等我怎样才能做到这一点?

I would like to be able to use the JSON object throughout the page so, for example, at any time I can just call topartists.artist[i].playcount; to display the playcount, etc. How can I do this?

推荐答案

在HTML变量必须的范围之内声明的每个

The html variable must be declared outside the scope of the each:

  //var topArt;
 $(document).ready(function() {
    $.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json&callback=?", function(data) {
        var html = '';
        $.each(data.topartists.artist, function(i, item) {
            html += "<p>" + item.name + " - " + item.playcount + "</p>";
        });
        $('#test').append(html);
         // topArt = data.topartists;
    });
});

关于你的第二个问题,你需要一个全局变量。你可以把它之前 $(文件)。就绪()(如图中的注释),这将是访问无处不在。

As for your second question, you'll need a global variable. You can put it before $(document).ready() (as shown in the comment) and it will be accessible everywhere.

这篇关于创建/访问使用jQuery $就与Last.FM API JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆