views:

17

answers:

1

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&lt;/image&gt;
        <image size="medium">http://userserve-ak.last.fm/serve/64/442288.jpg&lt;/image&gt;
        <image size="large">http://userserve-ak.last.fm/serve/126/442288.jpg&lt;/image&gt;
        <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.

+2  A: 

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!

Mark B
This works perfectly! Thanks so much for your help, I really appreciate it.
ben