1

The jQuery load function is nice but how do you handle session timeouts? I used it to add new content into a div but if my session times out, the login screen appears in that div instead of in the complete page.

Is there any way to handle this?

I am using it in JSP pages.

Should I replace it all by jsp:include pages and refresh the page every time I need to adjust the page or is there a way to do this with the jQuery load function and handle session timeouts properly?

I have tried validating the session before the call, but since there isn't any call to the server, the session still seems valid until the load request finds out it has expired. So session is not null in the current page, but when the load is executed to retreive the other page, it will get the login page instead.

gizmo753
  • 63
  • 2
  • 8

2 Answers2

1

I suppose your login page is a redirect. Then you can use the complete callback (third parameter, see doc) to check the response.

Andreas
  • 492
  • 5
  • 16
0

I encountered the same problem, and resolved it. So I am going to share my own answer.

At first I wanted to check if the response code is 301, but you cannot catch 301 redirect in the complete callback. It just throws 200, which is the result of redirection.

So I, instead, read the xhr.responseText and check if it starts with "<html>". Because if it is, it means that the response contains redirected login page instead of the modal contents.

 $(".modal-wrapper").load("/modal" + e, function(response, status, xhr){
      if (xhr.responseText.trim().startsWith("<html>")) {
          console.log("redirect to login page");
          window.location.href = "/";
      }
  });

Actually, instead of window.location.href = "/";, what I wanted to do is, document.write(xhr.responseText); so that the unnecessary request could not be made. But I counldn't achieve this due to some UI problems ;P

Hanee Park
  • 115
  • 1
  • 11