Get a score for a Reddit url using jQuery getJSON

I am trying to get an estimate for a specific url from reddit. To add the "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 invoke the URL using curl or your browser, the result looks as described in the Reddits API documentation . There is an array for children that contains a grade with a number.

$.getJSON , however, returns an empty response, observed when passing through it to firebug.

Perhaps this is a reddit protection method? Or am I using getJSON incorrectly?

+4
source share
1 answer

I am sure that you really should expect a JSONP response, in which case I'm sure you should add &callback=? to your URL.

EDIT: just a note for anyone facing this question in the future: jQuery getJSON() will fail if it encounters a malformed JSON object or, as in this case, receives a JSON object from another domain without JSONP padding. However, some browsers (e.g. Google Chrome) may trigger a warning such as MIME, which serves very well as an indication that something is wrong with the JSON object you are receiving.

+5
source

Source: https://habr.com/ru/post/1388663/


All Articles