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:
- The user selects the ‘Twitter’ checkbox in my app, and hits ‘share’.
- The app should then generate a url behind the scenes, and create a Tweet for the user’s account.
- The user can then be shown a verification box from Twitter, if necessary (presumably they’ll need to log in and verify the Tweet)
- The Tweet gets added to their Twitter feed.
It doesn’t sound very complicated, but I can’t find a way of doing it!
Edit
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?
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:
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.