Prateek Agarwal
Prateek Agarwal

Reputation: 417

Handle 302 status code upon HTML/JS request to server?

I'm using Require JS and Orace JET

How can we handle the 302 status code or any other status code returned by Server when the session is timeout.

Can it be configured to catch it and redirect the user to a login page upon such cases?

Upvotes: 0

Views: 3131

Answers (2)

ladar
ladar

Reputation: 5876

Here is my solution (that works because of luck). Take it just as an inspiration. When session in my app expires, the request is being forwarded using 302 to login page as you would expect. There are 2 main types of requests where it can happen:

  • REST API call (via XMLHttpRequest)
  • navigation on page that user did

For #2, the 302 poses no problem. User is simply redirected to login page.

For #1, I can somehow detect it (with some probability) because login page is on different domain that does not support CORS. The XMLHttpRequest request will simply fail with readyState equal to 0 and statusText equal to "error" (because browser will block the XMLHttpRequest to different domain). I have a "listener" (interceptor) on all my REST API calls and whenever some fails with the 0 readyState and "error" statusText, it means that session probably expired.

This is not a solution of course and cannot be used everywhere. Just an idea to think about :)

Upvotes: 2

Louis
Louis

Reputation: 151401

The status code 302 cannot be seen by RequireJS. That's just how browsers work: the browser will see 302 and automatically perform the next HTTP request. RequireJS won't know that there was a redirection.

Any HTTP status code that is an error (500, 400, etc.) will result in a module load failure and onError will be called.

Upvotes: 3

Related Questions