如何获得twitter URL计数



我有以下代码,我不能理解为什么它不返回并打印在HTML正文中…

var pageURL = document.URL;
var tweet  = "https://cdn.api.twitter.com/1/urls/count.json?url='"+ pageURL + "'";
$.getJSON(tweet,function(json){
    $('#twitterfeed').html(json.count); 
});
<div id="twitterfeed"></div>

https://cdn.api.twitter.com/1/urls/count.json?url=http://www.google.com

返回{"计数":23844636,"url":"http://www.google.com/"}

下面的内容似乎不起作用,有人知道为什么吗?

截至2015年11月20日,没有Tweet计数API,所以不要麻烦:https://blog.twitter.com/2015/hard-decisions-for-a-sustainable-platform

默认情况下,jQuery会发出AJAX请求。由于它是跨域的,并且响应中不存在CORS HTTP头,因此它失败了。添加和回调= ?请求URL来发出JSONP请求

  var pageURL = "http://www.google.com";
  var urlParams = $.param({ "url": pageURL });
  var tweet  = "https://cdn.api.twitter.com/1/urls/count.json?"+urlParams;

  $.ajax(tweet, { "dataType" :"jsonp" }).done(function(json){
    $('#twitterfeed').text(json.count); 
  });

演示

http://jsbin.com/tasaloraje/edit?html,输出

https://cdn.api.twitter.com/1/urls/count.json已弃用。

查看http://opensharecount.com的替代品,你可以把它改成那里提到的URL,如果你注册了你的域名。

最新更新