Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Counts for Twitter links, Facebook likes and Google +1 with Jquery and AJAX

Tags:

jquery

ajax

i am trying to get the counts for twitter, facebook and google with jquery.getJSON requests starting from the great guide by John Dyer on how do the same with C# and PHP

actually twitter and facebook works but google don't... the json response is:

{
  "error": {
  "code": 400,
  "message": "Required value: id",
  "data": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Required value: id"
   }
  ]
 },
 "id": "p"
}

here is the code:

<script type="text/javascript">
  function getfbcount(url){
     var fblikes;
     $.getJSON('http://graph.facebook.com/?ids=' + url, function(data){;
        fblikes = data[url].shares;
        $('body').append('fb likes ' + fblikes);
     });
  }
  function gettwcount(url){
     var tweets;
     $.getJSON('http://urls.api.twitter.com/1/urls/count.json?url=' + url + '&callback=?', function(data){
        tweets = data.count;
        $('body').append('tweets ' + tweets);
     });
  }
  function getplusone(url){
     var plusones;
     $.getJSON('https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ' + 'callback=?',
        {
           "method":"pos.plusones.get",
           "id":"p",
           "params":{
              "nolog":true,
              "id":url,
              "source":"widget",
              "userId":"@viewer",
              "groupId":"@self"
           },
           "jsonrpc":"2.0",
           "key":"p",
           "apiVersion":"v1"
        },
        function(data){           
        plusones = data.count;
        $('body').append('+1 ' + plusones);
     });
  }
  $(document).ready(function($){
        var url = "http://www.google.com"
        getfbcount(url);
        gettwcount(url);
        getplusone(url);
  });
  </script>
like image 791
alesmaru Avatar asked Oct 08 '22 23:10

alesmaru


2 Answers

you can't make cross domain requests to the clients6.google.com server. you can see this in your browsers console if you leave out this part:

+ 'callback=?'

twitter and facebook allow these requests...

like image 122
eikes Avatar answered Oct 18 '22 07:10

eikes


Do you just need an '&' before 'callback'? At the moment it looks like you are sticking it on to the end of the key.

like image 31
AnthonyVader Avatar answered Oct 18 '22 07:10

AnthonyVader