【问题标题】:Error in the file returned from a request to the Last.FM API从对 Last.FM API 的请求返回的文件中的错误
【发布时间】:2011-03-16 22:18:18
【问题描述】:

我正在使用带有 jquery 的 Last.fm API,如下所示:

$.getJSON('http://ws.audioscrobbler.com/2.0/?JSONCallback=?', {
    method: "user.getweeklytrackchart",
    user: "rj", 
    api_key: "fb04ae401284be24afba0fbc2f4b0efb"
}, function(data) {
    // console.debug (data)
});

我在 Firebug 中收到以下错误:

missing ; before statement
[Break on this error] <lfm status="ok">\n

单击错误会将我带到从请求返回的文件。错误发生在第 2 行(实际上还有更多的轨道对象,但我只包含了一个用于长度的对象):

<?xml version="1.0" encoding="utf-8"?>
<lfm status="ok">
    <weeklytrackchart user="RJ" from="1278244800" to="1278849600">
        <track rank="1">
            <artist mbid="309c62ba-7a22-4277-9f67-4a162526d18a">Beck</artist>
            <name>Mixed Bizzness</name>
            <mbid></mbid>
            <playcount>2</playcount>
            <image size="small">http://userserve-ak.last.fm/serve/34/442288.jpg</image>
            <image size="medium">http://userserve-ak.last.fm/serve/64/442288.jpg</image>
            <image size="large">http://userserve-ak.last.fm/serve/126/442288.jpg</image>
            <url>www.last.fm/music/Beck/_/Mixed+Bizzness</url>
        </track>
    </weeklytrackchart>
</lfm>

所以错误在返回的文件中,我该如何处理呢?感谢阅读。

【问题讨论】:

    标签: jquery json api last.fm


    【解决方案1】:

    这里有几个问题。

    首先,我认为您在请求网址末尾的JSONCallback=? 参数应该只是callback=?。虽然 jQuery docs 在示例代码中显示您的方式令人困惑,但在正文中却没有...

    另一件事是您正在使用getJSON 方法,而 Last.fm API 正在返回 XML,因此 jQuery 试图将返回的 XML 解析为 JSON,这显然无法做到。

    因此,您需要指定您希望返回 JSON 作为响应——这似乎可行:

    $.getJSON('http://ws.audioscrobbler.com/2.0/?callback=?', {
        method: "user.getweeklytrackchart",
        user: "rj",
        api_key: "fb04ae401284be24afba0fbc2f4b0efb",
        format: "json"
    }, function(data) {
        console.log(data);
    });
    

    您将能够在 Firebug 控制台中看到返回的 JSON 对象并检查它以查看您想要使用的数据。希望这会有所帮助!

    【讨论】:

    • 这很完美!非常感谢您的帮助,我真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2013-05-24
    • 1970-01-01
    相关资源
    最近更新 更多