質問

I am trying to get the score for a certain URL from reddit. To add a "share with reddit (count)" link:

function redditCounter(url) {
  // Get number of counts from reddit JSON api
  // $.getJSON('http://www.reddit.com/api/info.json?url='+url+'',
  $.getJSON('http://www.reddit.com/api/info.json?url=http://stackoverflow.com/q/811074/1288',
    function(data) {
      count = 0;
      if (data.children.count() > 0) {
        first_child = data.children[0];
        alert(first_child.score);
      }
    });
}

When you call the url with curl, or your browser, the result is like described in Reddits API documentation. There is a children array and that contains a score with a number.

The $.getJSON, however, returns an empty answer, seen when stepping over it in firebug.

Is this a protection method by reddit maybe? Or am I using getJSON wrong?

役に立ちましたか?

解決

I'm pretty sure you should actually be expecting a JSONP response, in which case I'm fairly certain you should append &callback=? to your URL.

EDIT: Just a note for anyone that stumbles across this question in the future: jQuery's getJSON() will fail silently if it encounters a malformed JSON object or, as in this case, receives a JSON object from another domain without the JSONP padding. However, some browsers (e.g: Google Chrome) may throw a MIME type warning that serves pretty well as an indicator that there's something wrong with the JSON object you're getting.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top