Get LinkedIn share count JSONP
Asked Answered
I

4

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!

Indiscrimination answered 3/1, 2015 at 19:3 Comment(1)
HI you should try using $.ajax cal and you do not need to specify the callback function in url. '$.ajax({ url: url, dataType: 'jsonp' })'Flavin
M
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>
Mephitic answered 3/1, 2015 at 19:27 Comment(5)
this would not work for linkedin. linkedin API are wierd and not formed properlyFlavin
its from experience I have been working with linkedin API like forever. jsonP does not work by defining the function to be called unless you do it using ajax call. you can try it yourselfFlavin
@viditbhatia Not able to view stacksnippets results , above ?Mephitic
sorry my bad. did not check it but it does not work in the api's i use .i would vote your answer upFlavin
Not sure if things have changed this past year, but this works great for LinkedIn as of today.Ima
I
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);
});
Indiscrimination answered 3/1, 2015 at 20:10 Comment(1)
Does not work anymore since LinkedIn updated their API to block callback names with numbersKerenkeresan
E
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);
});
Edgeways answered 16/3, 2015 at 17:21 Comment(2)
jQuery 1.5.1 is 4+ years old. Do you have a link to this recommended structure? Also, you need braces around the parameters to $.ajax().Cinematograph
This does not work anymore since LinkedIn modified their API to block callbacks with numbers in their nameKerenkeresan
K
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);;
        }
    });

Kerenkeresan answered 16/5, 2016 at 21:50 Comment(1)
The Answer provided by guest271314 still works for me. I appreciate your answer, but are you sure current solutions aren't working anymore?Indiscrimination

© 2022 - 2024 — McMap. All rights reserved.