0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sharon
  • 3,471
  • 13
  • 60
  • 93
  • 1
    Maybe [this][1] solution will be helpful [1]: http://stackoverflow.com/questions/7760662/how-to-send-tweet-to-twitter-with-jquery-inside-my-site – Karazyabko Sep 28 '12 at 11:04

1 Answers1

1

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.

Community
  • 1
  • 1
VDP
  • 6,340
  • 4
  • 31
  • 53
  • Thanks - I added some details above. – Sharon Sep 28 '12 at 12:13
  • Thanks. Where do I use the window.open() part? Sorry, I'm a bit unsure of myself when it comes to Twitter/Facebook APIs, and I get confused! – Sharon Sep 28 '12 at 14:08
  • The user selects the 'Twitter' checkbox in my app, and hits 'share'. -> in the on click handler for the share button, you check if the twitter checkbox is checked, if yes open up the window (with the generated url) – VDP Sep 28 '12 at 14:24
  • Is that before or after the AJAX request? Sorry for being dim. – Sharon Sep 28 '12 at 14:30
  • 1
    euhm what request? You don't have to do any requests :) – VDP Sep 28 '12 at 14:46
  • Ah, I see, so instead of the Ajax request, I just do window.open(serverURL) - that seems to be working - thanks! – Sharon Sep 28 '12 at 14:54