I am just trying to figure out the deferred api and have this nice sample code going with the twitter search api:
var getTweets = function(q) {
return $.ajax({
url: 'http://search.twitter.com/search.json?q=' + encodeURIComponent(q),
dataType: 'jsonp'
})
};
var getTheDay = function(date){
var date = new Date(date);
return date.getDay();
}
var parseTweetData = function(data){
$.each(data.results, function(index, tweet){
console.log(tweet.text
+ ' from ' + tweet.from_user_name
+ ' at ' + getTheDay(Date.parse(tweet.created_at) * 1000));
});
}
var parseError = function(error, xhr) {
alert('failed')
};
$.when(getTweets(' martin')).then(parseTweetData, parseError);
Getting results back is just fine. The problem comes with the situation where twitter returns a 403 error.
I want to handle that error with my custom error handler but that doesn’t seem to be triggered at all. What am I doing wrong? Have I misunderstood the api? How do I write a proper ajax error handler request?
According to the $.ajax docs the error handler isn’t called for jsonp requests, so it probably wont call in a deferred either.
you can try using a timeout to trigger an error callback, so basically we are giving the request some amount of time to be made and when the time expires we assume the request failed.