2

Using the LinkedIn API, I want to get the share count for an URL.

https://www.linkedin.com/countserv/count/share?url=http://www.linkedin.com&format=json

But this gives me an error because of Same-Origin Policy. I want to use JSONP to then get the data, but I am stuck there.

$.getJSON("https://www.linkedin.com/countserv/count/share?url=https://www.linkedin.com&format=jsonp&callback=myCallback", function(data) {
    elem.find(".count").html(data.count);
});

I still get the Same-Origin Policy error and no data from data.count. Can anyone help me out? Thanks!

B_s
  • 3,026
  • 5
  • 32
  • 51
  • HI you should try using $.ajax cal and you do not need to specify the callback function in url. '$.ajax({ url: url, dataType: 'jsonp' })' – vidit bhatia Jan 03 '15 at 19:30

4 Answers4

2

Try

myCallback = function(data) {
  // do stuff with `data`
};
var url = "https://www.linkedin.com/countserv/count/share?"
          + "url=https://www.linkedin.com&format=jsonp&callback=myCallback";
$.getScript(url);

See jQuery.getScript()

myCallback = function(data) {
  $("body").append("<pre>" + JSON.stringify(data, null, 2) + "</pre>")
};

$.getScript("https://www.linkedin.com/countserv/count/share?url=https://www.linkedin.com&format=jsonp&callback=myCallback");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
guest271314
  • 1
  • 15
  • 104
  • 177
2

Thanks everyone for your answers, but I solved it already myself.

This worked for me:

$.getJSON("http://www.linkedin.com/countserv/count/share?url=https://www.linkedin.com&format=jsonp&callback=?", function(data) {
        elem.find(".count").html(data.count);
});
B_s
  • 3,026
  • 5
  • 32
  • 51
  • 1
    Does not work anymore since LinkedIn updated their API to block callback names with numbers – frank May 16 '16 at 21:53
1

As of jQuery 1.5.1, this is the recommended way of structuring AJAX requests:

$.ajax({
    dataType: "jsonp",
    url: "http://www.linkedin.com/countserv/count/share",
    data: {
        callback: "?",
        format: "jsonp",
        url: "http://www.example.com/"
    }
}).done(function(data) {
    console.log(data.count);
});
bashaus
  • 1,614
  • 1
  • 17
  • 33
  • jQuery 1.5.1 is [4+ years old](http://blog.jquery.com/2011/01/31/jquery-15-released/). Do you have a link to this recommended structure? Also, you need braces around the parameters to `$.ajax()`. – Dan Dascalescu Mar 21 '15 at 00:57
  • This does not work anymore since LinkedIn modified their API to block callbacks with numbers in their name – frank May 16 '16 at 21:52
0

A few days ago, LinkedIn changed their API and the solutions above are broken :


    $.getJSON("http://www.linkedin.com/countserv/count/share?url=https://www.linkedin.com&format=jsonp&callback=?", function(data) {
            elem.find(".count").html(data.count);
    });

fails because jQuery will replace the ? into a callback with a random name with numbers in it. And Linkedin now prevents using numbers in callback names.

The solution is to use to call "manually" $.ajax to prevent jQuery automation


    $.ajax({
        dataType: "jsonp",
        url: 'https://www.linkedin.com/countserv/count/share',
        data: {'url':encodeURIComponent(location.href)},
        jsonpCallback: 'this_is_a_random_name',
        success: function(data){
                elem.find(".count").html(data.count);;
        }
    });

frank
  • 438
  • 1
  • 5
  • 19
  • The Answer provided by guest271314 still works for me. I appreciate your answer, but are you sure current solutions aren't working anymore? – B_s May 19 '16 at 15:46