1

First of all, here is my basic code:

    <div id="example-ontweet"></div>

<script type="text/javascript">

  twttr.anywhere(function (T) {

    T("#example-ontweet").tweetBox({

      onTweet : function(plaintext, html) {

        alert(plaintext);
        alert(html);

      }

    });

  });

</script>

When I send I submit a tweet, it works fine like it should. When I try to submit a duplicate tweet, it doesn't allow it, and the loading spinner just spins with no error message--also default behavior. When I look in the firebug console upon duplicate tweet submit, I see:

POST https://api.twitter.com/1/statuses/update.json 403 forbidden

and under the Response console tab in there is this info:

{"error":"Status is a duplicate.","request":"\/1\/statuses\/update.json"}

All I want to do is alert that response message on fail--either the whole object, or preferably just the "error." I tried:

me.tweetBox.$button.click(function(){   
    $.getJSON('https://api.twitter.com/1/statuses/update.json', function(data) {
        alert(JSON.stringify(data));
    });
});

Both inside of a settimeout function and not, and it looked like it was going to work, but it spit this out below into the console when it tried:

GET https://api.twitter.com/1/statuses/update.json    401 Unauthorized

Any ideas of how to pull that error message into an alert? It seems like it should be so simple, but I'm banging my head against the wall. Thanks

Ivan Durst
  • 1,163
  • 2
  • 12
  • 24

2 Answers2

0

Try

me.tweetBox.$button.click(function(){   
    $.getJSON('https://api.twitter.com/1/statuses/update.json', function(data) {
        alert(JSON.stringify(data));
    }).error(function(jqXHR, textStatus, errorThrown){
        alert(jqXHR.responseText);
    });
});
Musa
  • 96,336
  • 17
  • 118
  • 137
  • Thanks for the reply Musa. Now I do get an alert that pops up, but it is blank, and in console it still says `GET https://api.twitter.com/1/statuses/update.json 401 Unauthorized` I set it inside a timeout function to make sure it executes after the response, but still blank. Any thoughts? – Ivan Durst Sep 13 '12 at 00:55
  • @IvanDurst do a `console.log(jqXHR)` in `.error` and see what property of `jqXHR` have the information you want. – Musa Sep 13 '12 at 00:57
  • Musa, I think the issue I'm having is that the json is coming from a different url. I'm not even sure I can pull that data directly in from twitter through Twitter Anywhere, though I thought since it's logged in the console, I could display it on my page. I tried adding `callback=?` to the end of the json url, but to no avail. Basically, I just want to display this string from the twitter json: `"error":"Status is a duplicate."` (or whatever else "error" may be). Thanks for the help – Ivan Durst Sep 13 '12 at 22:19
0

I believe the new changes to Twitter's API doesn't allow duplicate messages.

https://dev.twitter.com/docs/api/1.1/post/statuses/update

For each update attempt, the update text is compared with the authenticating user's recent tweets. Any attempt that would result in duplication will be blocked, resulting in a 403 error. Therefore, a user cannot submit the same status twice in a row.

Patriotec
  • 1,104
  • 4
  • 22
  • 43