最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get twitter URL count? - Stack Overflow

programmeradmin3浏览0评论

I've got the following code, and I cannot understand as to why it isn't returning and printing it in the HTML body..

var pageURL = document.URL;
var tweet  = ".json?url='"+ pageURL + "'";

$.getJSON(tweet,function(json){
    $('#twitterfeed').html(json.count); 
});

<div id="twitterfeed"></div>

.json?url=

returns {"count":23844636,"url":"/"}

The following doesn't seem to work, does anyone have any idea as to why?

I've got the following code, and I cannot understand as to why it isn't returning and printing it in the HTML body..

var pageURL = document.URL;
var tweet  = "https://cdn.api.twitter./1/urls/count.json?url='"+ pageURL + "'";

$.getJSON(tweet,function(json){
    $('#twitterfeed').html(json.count); 
});

<div id="twitterfeed"></div>

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

returns {"count":23844636,"url":"http://www.google./"}

The following doesn't seem to work, does anyone have any idea as to why?

Share Improve this question edited Aug 25, 2015 at 0:25 Maksym Kozlenko 10.4k2 gold badges68 silver badges55 bronze badges asked Aug 24, 2015 at 0:37 iBrazilian2iBrazilian2 2,2936 gold badges24 silver badges45 bronze badges 3
  • Is the JavaScript before the HTML in the same file? – user2182349 Commented Aug 24, 2015 at 0:42
  • Yes, It's on the top. – iBrazilian2 Commented Aug 24, 2015 at 0:44
  • Wrap it in $(function(){ // your code here }); which will delay its execution until the DOM has been loaded and parsed. – user2182349 Commented Aug 24, 2015 at 0:45
Add a ment  | 

3 Answers 3

Reset to default 10

As of 20th November 2015 there's no Tweet count API, so don't bother: https://blog.twitter./2015/hard-decisions-for-a-sustainable-platform

By default jQuery makes AJAX request. Since it's cross domain and CORS HTTP header is not present in response it fails. Add &callback=? to request URL to make JSONP request

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


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

Demo

http://jsbin./tasaloraje/edit?html,output

https://cdn.api.twitter./1/urls/count.json is deprecated.

Check out http://opensharecount. for a replacement though, you can just change it to the URL mentioned there if you sign up for your domain.

发布评论

评论列表(0)

  1. 暂无评论