Reputation: 3909
I'm trying to allow users to tweet a link from within my jQuery app. Basically, they create their own page, and then they select a number of social networks to share it on. On clicking 'Share', the app loops over the social network checkboxes, and posts to each, as selected.
I'm struggling to find a good way to do this for Twitter - everywhere I look online, I seem to get conflicting advice.
So, basically, what should happen is this:
It doesn't sound very complicated, but I can't find a way of doing it!
Thanks to the responses below, I've now got the following code:
var serverUrl = 'https://twitter.com/intent/tweet?url="www.rocketreel.com"&text="Test"';
$.ajax({
type: "GET",
url: serverUrl,
success: function(data) {
},
error: function(data) {
var obj = jQuery.parseJSON(data);
alert(obj.error);
}
});
Now when I run it, I get (in the console):
XMLHttpRequest cannot load https://twitter.com/intent/tweet?url=%22www.mysite.com%22&text=%22Test%22. Origin null is not allowed by Access-Control-Allow-Origin.
I'm not able to run it from the actual site at the moment, so am using localhost - is that why I'm getting this?
Upvotes: 0
Views: 2965
Reputation: 6420
Have you tried reading the api documentation?
https://dev.twitter.com/docs/tweet-button
https://twitter.com/about/resources/buttons#tweet
You can check the code of the tweet button and execute the code behind the tweet button. Pretty straight forward...
Here is the url that's being called:
https://twitter.com/intent/tweet?original_referer=yoururl.com&source=tweetbutton&url=http://urltotweet.com&text=[text to tweet]
UPDATE:
You are running into a cross origin issue. You can't load data from an other origin through ajax unless you use some workarounds (CORS, proxy,... since you can't modify the twitter api, a proxy is your only option. But that too can come with it's complications..) you probably don't even need.
Just open up a new window with the location... window.open(...) or create an iframe with the provided url in staid of loading a complete page through ajax.
Upvotes: 1