kinet
kinet

Reputation: 1780

Handling Twitter API Errors with jQuery $.getJSON

$.getJSON(twitter_url, function(data){ 
                          loadtwit(data);
                });

I am querying the twitter url via $.getJSON, for instance: http://twitter.com/statuses/friends/stevejobs.json?callback=?

Sometimes the browser response is UnAuthorized if the tweets are protected. How can I handle that response to avoid triggering a login form.

success: loadtwit(data);

else: die silently

Upvotes: 2

Views: 1147

Answers (2)

kinet
kinet

Reputation: 1780

I think I found something close to a workaround, if not an answer finally.

In my case I wanted to show a user's location, name, photo and some tweets based on their username which I knew, so I was trying to use this:

http://api.twitter.com/1/statuses/user_timeline.json?&screen_name=stevejobs&count=20&callback=?

Which trigger the popup that looks like a phishing scam when querying users with protected tweets.

So you can query a search for tweets based on a user like this:

http://search.twitter.com/search.json?q=from:stevejobs&callback=?

And you can also query a user like this:

http://api.twitter.com/1/users/show/stevejobs.json?&callback=?

The first query will get back tweets and won't ask for passwords because the protected users do not appear in search. Works, but doesn't return location and meta data for the user.

The 2nd query doesn't return tweets, but does return a boolean value for protected.

So if you put it together, you can get a complete search.

I hope someone finds this useful. I've been googling and reading the API all day. After I write the function, I'll come back here and post it.

Upvotes: 0

sje397
sje397

Reputation: 41822

You can use $.ajax and provide an error handler.

$.ajax({
  url: twitter_url,
  dataType: 'json',
  success: function(data) {
    loadtwit(data);
  },
  error: function(xhr, testStatus, error) {
    // handle error
  }
});

Upvotes: 2

Related Questions