开发者

Trouble with jQuery's JSON Method

$(function(){
    $.getJSON('http://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=ElicitBelief&api_key=25135a535781328243f9e31968abc14&format=json', function(data) {
      alert(data)
    });
});

Firebug says: GET http://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=ElicitBelief&api_key=25135a535781328243f9e31968abc14&format=json 200 OK 144ms and the URL is red, so it's presumebly not fetching the data 开发者_开发问答at all.

I can't think what the problem is.


Presumably the url is in a different domain. You'll need to use JSONP and add &jsoncallback=? to your query. I assume that the audioscrobbler API supports this.


Visiting that URL gives me a file that says, "Invalid API key - You must be granted a valid key by last.fm"


According to that link http://www.ibm.com/developerworks/library/wa-aj-jsonp1/, you have to add an "&callback=?" at the end of your URL (reason for this, is that jQuery automaticly uses JSONP to bypass AJAX Cross Domain policy when using $.getJSON).

So, if you pass below url to getJSON: http://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=ElicitBelief&api_key=25135a535781328243f9e31968abc14&format=json&callback=? you'll get JSON respoonse with : "Invalid API key - You must be granted a valid key by last.fm"

Check this for demo: JSBin (page is empty, but look at requests in Firebug console)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜