How to get twitter url url?

I have the following code and I can’t understand why it doesn’t return and print it in the body of 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

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

The following doesn't seem to work, does anyone know why?

+6
source share
3 answers

By default, jQuery executes an AJAX request. Since this is a cross-domain and the CORS HTTP header is not in response, it fails. Add & callback =? request url to make jsonp request

  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); }); 

Demo

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

+3
source

As of November 20, 2015 there are no reading APIs, so don't worry: https://blog.twitter.com/2015/hard-decisions-for-a-sustainable-platform

+10
source

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

Check out http://opensharecount.com for a replacement, but you can simply change it to the URL provided here if you sign up for your domain.

+2
source

All Articles