P
P
Platon Fedorovich2015-09-26 13:11:48
JSON
Platon Fedorovich, 2015-09-26 13:11:48

How to get the view count for the current URL from StumbleUpon (using jQuery.getJSON)?

Greetings!
I've been looking for a solution to this issue for quite some time now. The bottom line is this: you need to get the number of “shares” from the StumbleUpon service into your script (custom share buttons) via jQuery.getJSON.
Yes, the service has great documentation on this topic , but (for unknown reasons) what is written there does not work. No, well, it works, of course, through the address bar of the browser .. but there is no way to get the value through jQuery.getJSON - an error occurs in the chrome console.
Roughly speaking, if you enter an example from their API in the browser:

http://www.stumbleupon.com/services/1.01/badge.getinfo?url=http://www.treehugger.com/

then a great valid JSON is produced:
{
    "result": {
        "url": "http://www.treehugger.com/",
        "in_index": true,
        "publicid": "1tin26",
        "views": 123623,
        "title": "TreeHugger",
        "thumbnail": "http://cdn.stumble-upon.com/mthumb/428/925428.jpg",
        "thumbnail_b": "http://cdn.stumble-upon.com/bthumb/428/925428.jpg",
        "submit_link": "http://www.stumbleupon.com/badge/?url=http://www.treehugger.com/",
        "badge_link": "http://www.stumbleupon.com/badge/?url=http://www.treehugger.com/",
        "info_link": "http://www.stumbleupon.com/url/www.treehugger.com/"
    },
    "timestamp": 1443261036,
    "success": true
}

But if you try to access via jQuery.getJSON from the script, then all the time there will be this error:
badge.getinfo?url=http%3A%2F%2Fwww.treehugger.com%2F&callback=jQuery111203496418842114508_14…:1
Uncaught SyntaxError: Unexpected token :

At the same time, my call script is as follows:
...
$.getJSON('http://www.stumbleupon.com/services/1.01/badge.getinfo?url=' + encodeURIComponent('http://www.treehugger.com/') + '&callback=?', function(response) {
  console.log(response);
});
...

Yes, there are solutions using various third-party services, such as YQL or Any Origin (and its open source alternative - Whatever Origin ), through which everything works .. but this is an extra request and waiting time.
Also, YQL has a limit on requests per day (per hour), and Any Origin is generally paid and can be banned “without trial or investigation” for free use (which is allowed there for review). But with Whatever Origin, almost everything worked out, but the 404th error of the StumbleUpon service constantly comes up in the request instead of the beautiful JSON (which is higher) .. so, also by.

Still, the main question is: why doesn't it work directly through jQuery.getJSON?!
Tell me what I'm doing wrong (maybe there is a solution case?).. otherwise all the links on this topic are already in Google "visited color" :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Ineshin, 2015-09-26
@IonDen

Because cross-domain request. Not everything is so simple because of the security policy. Read about CORS and JsonP

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question