从对 Last.FM API 的请求返回的文件中出现错误

发布于 2024-09-09 23:41:37 字数 1389 浏览 3 评论 0原文

我将 Last.fm API 与 jquery 结合使用,如下所示:

$.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>

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

I'm using the Last.fm API with jquery as follows:

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

I'm getting the following error in Firebug:

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

Clicking on the error takes me to the file that gets returned from the request. The error is occurring in line 2 (there are actually many more of the track objects but I've only included one for length):

<?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>

So the error is in the returned file, how do I deal with it? Thanks for reading.

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

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

发布评论

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

评论(1

假装爱人 2024-09-16 23:41:37

这里有一些问题。

首先,我认为请求 URL 末尾的 JSONCallback=? 参数应该是 callback=?。尽管令人困惑的是 jQuery 文档 在示例代码中显示了您的方式,但在主体中却没有显示text...

另一件事是您正在使用 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 对象,并检查它以了解您想要使用哪些数据。希望这有帮助!

There are a couple of things wrong here.

First, I think the JSONCallback=? parameter you have on the end of the request url should just be callback=?. Although confusingly the jQuery docs show your way in the example code, but not in the main body text...

The other thing is that you are using the getJSON method, while the Last.fm API is returning XML, so jQuery is attempting to parse that returned XML as JSON, which it obviously can't do.

So you need to specify that you'd like JSON back as a response—this seems to work:

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

You will be able to see the returned JSON object in the Firebug console and examine it to see which data you want to use. Hope this helps!

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