Search code examples
javascriptjqueryjsonapiaddthis

can't get shares count data from addThis API using <script/>


After trying to find a solution for this problerm in a couple hours. I decided to ask the experts who are on Stackoverflow.

Using the same script code. It's working for Facebook API, but not working for addThis API. Anyone can help?

I just need addThis Total Shares Count, and put in on my HTML element on my site.

Thanks a lot.

  var sum = 0,
      link = encodeURI('http://twitter.com');


$.getJSON('http://api-public.addthis.com/url/shares.json?url='+link,function(addthis) {

    sum += parseFloat(addthis.shares);

    $('.at').html(sum);

  });

  $.getJSON('http://graph.facebook.com/?id='+link,function(fb) {

    sum += parseFloat(fb.shares);

    $('.fb').html(sum);

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class='fb'>fb script not working.</div>

<div class='at'>addThis script not working.</div>


Solution

  • I believe you are making a XHR request from a different domain that what you are on. Hence the issue here is CORS. When you will try making that request than on the browser console you should get the error

    No 'Access-Control-Allow-Origin' header is present on the requested resource.

    Modern browsers block Cross origin requests due to security reasons. For better understanding read more on that here