# How to catch AJAX calls on session-timeout?

**URL:** <https://www.ruby-forum.com/t/how-to-catch-ajax-calls-on-session-timeout/162149>\
**Category:** Rails\
**Created:** [March 6, 2009, 9:31am UTC](https://www.ruby-forum.com/t/how-to-catch-ajax-calls-on-session-timeout/162149 "2009-03-06T09:31:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [March 6, 2009, 9:31am UTC](https://www.ruby-forum.com/t/how-to-catch-ajax-calls-on-session-timeout/162149/1 "2009-03-06T09:31:41Z")

</div>

hallo everybody,

my problem is the following:  
on one of our rails-apps we implemented session-expiry, so that the  
user gets redirected to our login-page whenever his session has timed-  
out. this is working fine for normal http-requests. however, for AJAX-  
calls that only update a certain div of the page it is not! the  
redirection works, but (as one would expect) only that specified div  
gets updated, so our login page gets rendered inside the div.

i’m looking for a solution that reloads the whole page and shows the  
login-screen without having to include an on-failure-section in all of  
our AJAX-calls (because there are many of them). surely there must be  
approaches on that subject already. but it’s hard to google without  
some keywords to look for.

thanks  
MaD

---

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [March 6, 2009, 11:39am UTC](https://www.ruby-forum.com/t/how-to-catch-ajax-calls-on-session-timeout/162149/2 "2009-03-06T11:39:52Z")

</div>

On Mar 6, 8:31 am, MaD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i’m looking for a solution that reloads the whole page and shows the  
> login-screen without having to include an on-failure-section in all of  
> our AJAX-calls (because there are many of them). surely there must be  
> approaches on that subject already. but it’s hard to google without  
> some keywords to look for.

This happens because the browser handles the redirect without the  
javascript ever seeing it. The way I handle it is that if the request  
is an ajax one and they are no longer logged in then I set a header  
instead of redirecting. The client side code. In my app all ajax  
requests are made by javascript object that derive from a common base  
class and which basically have a callback chain of onCompletes one of  
which checks this, so I don’t need to repeat this for every ajax  
request. You can probably also do it with an Ajax.Responder

Fred

---

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [March 6, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/how-to-catch-ajax-calls-on-session-timeout/162149/3 "2009-03-06T13:01:11Z")

</div>

i’m gonna try that. thanks!
